前文:Centos7+Nginx+PHP 基础WEB运行环境手工部署php
此前咱们在Centos7上手工部署了Nginx+PHP的运行环境,然而并无说到虚拟主机多个站点的部署方法,此文将继续记录Nginx虚拟主机多站点的一些部署及注意细节。html
Nginx安装路径:/usr/local/nginxnginx
Nginx主配置:/usr/local/nginx/nginx.confweb
默认网站目录:/usr/local/nginx/htmlbash
若是您的配置和当前的配置不同,注意将文中路径替换。php7
建立网站目录以及配置目录php-fpm
#建立网站目录及网站产生的日志存放目录 mkdir /mnt/web/example/wwwroot -p mkdir /mnt/web/example/log -p #建立 nginx 加载的虚拟主机配置存放目录 mkdir /usr/local/nginx/vhost #增长配置文件引入 vi /usr/local/nginx/nginx.conf #在 http 段尾部增长 include /usr/local/nginx/vhost/*.conf; #建立默认文件 echo "<?php phpinfo();>" > /mnt/web/example/wwwroot/index.php echo "hi example.com" > /mnt/web/example/wwwroot/index.html #设置权限 chown -R php-fpm:www /mnt/web chmod -R 775 /mnt/web
增长一个网站配置测试
cd /usr/local/nginx/vhost vi example.conf
配置文件内容以下网站
log_format soshash.log.format '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; server { listen 80; server_name example.com www.example.com *.demo.example.com; index index.html index.htm index.php; root /mnt/web/example/wwwroot; access_log /mnt/web/example/log/access.log example.log.format; error_log /mnt/web/example/log/error.log; }
域名绑定(server_name):spa
默认文件(index):按照优先顺序显示默认网页。
网站目录(root):填写咱们预先建立的网站目录。
访问日志文件(access_log):
错误日志文件(error_log):
重载nginx配置
/usr/local/nginx/nginx -s reload
解析域名并测试访问
http://www.example.com/index.html 有效
http://www.example.com/index.php 错误(下载资源文件)
显然是咱们的虚拟主机没有对PHP文件进行加载执行
给虚拟主机文件增长配置,以下:
log_format soshash.log.format '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; server { listen 80; server_name example.com www.example.com *.demo.example.com; index index.html index.htm index.php; root /mnt/web/example/wwwroot; #新增配置以下 location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; include fastcgi_params; } access_log /mnt/web/example/log/access.log example.log.format; error_log /mnt/web/example/log/error.log; }
重载nginx
/usr/local/nginx/nginx -s reload
再次测试经过。
类同,配置其余多个虚拟主机也同样如此简单。
对于多版本PHP的话,只须要将其余PHP编译安装到另一个目录,配置网站时监听对应的端口便可。
如:/usr/local/php/php7/
修改配置:php-fpm.conf
listen = 127.0.0.1:9001
对应nginx虚拟主机的配置更改
location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9001;#不一样端口对应不一样php版本 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; include fastcgi_params; }
异同之处只有这些,配置起来是比较简单的。