环境
Virtualbox最小化安装 Centos 7
yum update
timedatectl set-timezone Asia/Hong_Kong
timedatectl
vi /etc/selinux/config
修改如下内容:
SELINUX=disabled
#SELINUXTYPE=targeted //加#注释
重启系统使其生效
shutdown -r now
在http://nginx.org/packages/centos/7/noarch/RPMS/查看最新库信息
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
vi /etc/nginx/nginx.conf
查找gzip,取消注释,修改为:
gzip on;
vi /etc/nginx/conf.d/default.conf
把server{}段全部注释掉,再添加如下内容(用于屏蔽80端口空主机头访问)
server {
listen 80 default;
return 500;
}
查看系统firewall防火墙服务是否开启,若已开启,需要修改防火墙配置,
systemctl status firewalld
若输出active(runing),则需要调整防火墙规则的配置
vi /etc/firewalld/zones/public.xml
在zone一节中添加
<zone>
...
<service name="http"/>
<zone>
保存后重启firewalld服务
systemctl reload firewalld
配置防火墙开启HTTP服务端口
firewall-cmd --permanent --add-service=http
-permenent参数表示这是一条永久防火墙规则,如果不加则重启系统后就没有这条规则了
重启Firewalld使该规则生效
systemctl restart firewalld
加入后的防火墙规则为:
# firewall-cmd --list-all
public (default, active)
interfaces: enp0s3
sources:
services: dhcpv6-client http ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
systemctl start nginx.service
systemctl enable nginx.service
nginx主配置文件:/etc/nginx/nginx.conf
nginx默认配置文件目录:/etc/nginx/conf.d/
nginx默认站点主目录:/usr/share/nginx/html/
nginx默认日志目录:/var/log/nginx/
yum install mariadb mariadb-server net-tools
systemctl start mariadb.service
systemctl enable mariadb.service
mysql_secure_installation
这个脚本会经过一些列的交互问答来进行MariaDB的安全设置。
首先提示输入当前的root密码:
Enter current password for root (enter for none):
初始root密码为空,我们直接敲回车进行下一步。
Set root password? [Y/n]
设置root密码,默认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。
Remove anonymous users? [Y/n]
是否移除匿名用户,默认选项为Yes,建议按默认设置,回车继续。
Disallow root login remotely? [Y/n]
是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按默认设置,回车继续。 如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。
Remove test database and access to it? [Y/n]
是否删除测试用的数据库和权限? 建议按照默认设置,回车继续。
Reload privilege tables now? [Y/n]
是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。
完成后你会看到Success!的提示,MariaDB的安全设置已经完成。我们可以使用以下命令登录MariaDB:
mysql -u root -p
按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。
yum -y install php-fpm php-cli php-mysql php-gd php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap
pecl install apc
后会出现配置提示,所有选择项全部输入回车
yum -y install php-devel
yum -y groupinstall 'Development Tools'
pecl channel-update pecl.php.net
pecl install apc
说明:APC服务用于加速php
vi /etc/php.ini
查找expose_php,修改为以下内容(隐藏php版本号):
expose_php = Off
查找cgi.fix_pathinfo和date.timezone,修改为以下内容:
cgi.fix_pathinfo = 0
date.timezone = "Asia/Hong_Kong"
查找Dynamic Extensions,在该配置区块插入以下内容:
extension=apc.so
vi /etc/php-fpm.d/www.conf
查找listen = 127.0.0.1:9000,修改为以下内容
listen = /var/run/php-fpm/php-fpm.sock
查找user = apache,修改为以下内容
user = nginx
查找group = apache,修改为以下内容
group = nginx
systemctl enable php-fpm.service
systemctl start php-fpm.service
mkdir -p /data/test/web
mkdir -p /data/test/log
mkdir -p /data/test/tmp/session
useradd -d '/data/test' -s /sbin/nologin test
passwd test
usermod -G nginx test
chown -R test:nginx /data/test
vi /etc/nginx/conf.d/test.conf
输入以下内容:
server {
listen 80;
server_name www.test.com;
access_log /data/test/log/access.log;
error_log /data/test/log/error.log;
root /data/test/web;
index index.php index.html index.htm;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on sock
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/test.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Deny all attempts to access hidden files such as .htaccess
# Deny access to any files with a .php extension in the uploads directory
#
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location ~* \.(gif|jpg|jpeg|png|bmp|txt|zip|jar|swf)$ {
expires 30d;
access_log off;
valid_referers none blocked *.test.com server_names ~\.google\. ~\.baidu\. ~\.bing\. ~\.yahoo\. ~\.soso\. ~\.sogou\. ~\.alexa\. ~\.haosou\. ~\.youdao\.;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://www.test.com/403.png;
}
}
rewrite ^/sitemap.xml$ /sitemap.php last;
}
server {
server_name test.com;
rewrite ^/(.*)$ http://www.$host/$1 permanent;
}
说明:监听80端口,自定义日志文件存放位置,对favicon.ico和robots.txt的访问及错误不写入日志,启用php-fpm支持且使用test.sock套接字通信,屏蔽对.开头的隐藏文件的访问(比如.htaccess),屏蔽对uploads和files目录下php文件的访问(通常是上传文件存放目录),图片防盗链,访问sitemap.xml文件改写为访问sitemap.php,访问test.com重定向到www.test.com。
如果站点同时使用http和https,要把配置文件开头部分改为:
server {
listen 80;
listen 443 ssl;
server_name www.test.com;
ssl_certificate /data/test/crt/www.test.com.crt;
ssl_certificate_key /data/test/crt/www.test.com.key;
增加对443端口ssl模式的监听,指定ssl证书和密钥的位置。站点同时使用http和https时,页面文件调用本站资源可以去掉http:或者https:,只保留后面的内容(//…),浏览器能自动匹配相应的头部。
如果要强制使用https,把http访问都转到https,则修改配置文件开头和结尾如下:
server {
listen 443 ssl;
server_name www.test.com;
ssl_certificate /data/test/crt/www.test.com.crt;
ssl_certificate_key /data/test/crt/www.test.com.key;
...
}
server {
listen 80;
server_name www.test.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
server_name test.com;
rewrite ^/(.*)$ https://www.$host/$1 permanent;
}
测试nginx配置文件是否正确
nginx -t
输出如下:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
vi /etc/php-fpm.d/test.conf
输入如下内容
[test]
listen = /var/run/php-fpm/test.sock
listen.allowed_clients = 127.0.0.1
listen.owner = test
listen.group = nginx
listen.mode = 0660
user = test
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
chdir = /
slowlog = /var/log/php-fpm/www-slow.log
php_value[session.save_handler] = files
php_value[session.save_path] = /data/test/tmp/session
php_admin_value[open_basedir] = /data/test/web:/data/test/tmp:/usr/share/php:/tmp
php_admin_value[upload_tmp_dir] = /data/test/tmp
vi /etc/logrotate.d/test
输入以下内容
/data/test/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
logrotate说明待添加
systemctl restart nginx.service
systemctl restart php-fpm.service
建立配置文件的备份文件夹
mkdir -p /etc/nginx/conf.bak
mkdir -p /etc/php-fpm.bak
把站点test的配置文件移走并重启nginx和php-fpm服务,就关闭了站点test
mv /etc/nginx/conf.d/test.conf /etc/nginx/conf.bak/
mv /etc/php-fpm.d/test.conf /etc/php-fpm.bak/
systemctl restart nginx.service
systemctl restart php-fpm.service
把配置文件移回并重启nginx和php-fpm服务,就重开了站点test
mv /etc/nginx/conf.bak/test.conf /etc/nginx/conf.d/
mv /etc/php-fpm.bak/test.conf /etc/php-fpm.d/
systemctl restart nginx.service
systemctl restart php-fpm.service
新建用户、站点文件夹和配置文件,重启服务。
yum install epel-release
yum install phpmyadmin
vi /etc/nginx/conf.d/phpmyadmin.conf
输入以下内容
server {
listen 80;
server_name phpmyadmin.test.com;
root /usr/share/phpMyAdmin;
index index.php index.html index.htm;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on sock
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Deny all attempts to access hidden files such as .htaccess
# Deny access to any files with a .php extension in the uploads directory
#
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
创建session目录,添加Nginx权限,重启php-fpm:
mkdir -p /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session
systemctl restart php-fpm
systemctl restart nginx.service
vi /etc/hosts
添加如下:
127.0.0.1 test.com www.test.com
方法一:修改域名解析把phpmyadmin.test.com指向服务器IP
vi /etc/hsots
添加如下:
127.0.0.1 phpmyadmin.test.com
浏览器打开http://phpmyadmin.test.com就可以使用phpmyadmin,长期不用可关闭此站点。
方法二:也可以不建立此站点,而把phpmyadmin目录软链接到站点test目录下调用
ln -s /usr/share/phpMyAdmin /data/test/web/phpmyadmin
打开http://www.test.com/phpmyadmin即可,前提是站点test未关闭。
不用的时候删除这个软链接。
首次登陆phpmyadmin提示
設定檔案需要設定一組加密密碼 (blowfish_secret)。
修改phpMyAdmin配置文件:
vi /usr/share/phpMyAdmin/libraries/config.default.php
查找
$cfg[‘blowfish_secret’]
修改为
$cfg[‘blowfish_secret’] = ‘test’;
首次登陆phpmyadmin提示
尚未設定 phpMyAdmin 設定儲存空間,部份延伸功能將無法使用。 了解原因。 或者前往任一個資料庫的 ‘操作’ 頁籤設定。
执行以下命令(需要输入phpMyAdmin的root密码):
cd /usr/share/phpMyAdmin/sql/
mysql -uroot -p < create_tables.sql
至此,LNMP配置完毕。
Virtualbox下Centos 7配置LAMP:Centos7配置LAMP
VPS Centos 6配置LAMP:VPS Centos 6配置LAMP
VPS Centos 6配置LAMP:VPS Centos 6配置LNMP