Centos7+Nginx+PHP 基础WEB运行环境-多虚拟主机配置

前端之家收集整理的这篇文章主要介绍了Centos7+Nginx+PHP 基础WEB运行环境-多虚拟主机配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前文:Centos7+Nginx+PHP 基础WEB运行环境手工部署

此前我们在Centos7上手工部署了Nginx+PHP的运行环境,然而并没有说到虚拟主机多个站点的部署方法,此文将继续记录Nginx虚拟主机多站点的一些部署及注意细节。

Nginx安装路径:/usr/local/Nginx

Nginx主配置:/usr/local/Nginx/Nginx.conf

默认网站目录:/usr/local/Nginx/html

如果您的配置和当前的配置不一样,注意将文中路径替换。

准备工作

创建网站目录以及配置目录

  1. #创建网站目录及网站产生的日志存放目录
  2. mkdir /mnt/web/example/wwwroot -p
  3. mkdir /mnt/web/example/log -p
  4.  
  5. #创建Nginx加载的虚拟主机配置存放目录
  6. mkdir /usr/local/Nginx/vhost
  7.  
  8. #创建默认文件
  9. echo "<?PHP PHPinfo();>" > /mnt/web/example/wwwroot/index.PHP
  10. echo "hi example.com" > /mnt/web/example/wwwroot/index.html
  11.  
  12. #设置权限
  13. chown -R PHP-fpm:www /mnt/web
  14. chmod -R 775 /mnt/web

配置文件

普通虚拟主机配置(不支持PHP

增加一个网站配置

  1. cd /usr/local/Nginx/vhost
  2. vi example.conf

配置文件内容如下

  1. log_format soshash.log.format '$remote_addr - $remote_user [$time_local] $request'
  2. '$status $body_bytes_sent $http_referer '
  3. '$http_user_agent $http_x_forwarded_for';
  4. server {
  5. listen 80;
  6. server_name example.com www.example.com *.demo.example.com;
  7. index index.html index.htm index.PHP;
  8. root /mnt/web/example/wwwroot;
  9. access_log /mnt/web/example/log/access.log example.log.format;
  10. error_log /mnt/web/example/log/error.log;
  11. }

域名绑定(server_name):

  • 单域名:server_name www.example.com
  • 多域名:server_name www.example.com PHP.example.com
  • 泛域名:server_name *.demo.example.com
  • 以及正则匹配域名。域名可以绑定多个,只需要用空格分割开即可。

默认文件(index):按照优先顺序显示默认网页。

网站目录(root):填写我们预先创建的网站目录。

访问日志文件(access_log):

  • access_log 产生日志文件存储路径 日志内容的格式(example.log.format)
  • example.log.format 相当于变量一样,需要提前声明。
  • 最新版本的 Nginx(1.12.0)需要 将log_format 放置到 server段外部,否则会报一个类似:Nginx: [emerg] "log_format" directive is not allowed here in xxx 的错误

错误日志文件(error_log):

  • #error_log logs/error.log;
  • #error_log logs/error.log notice;
  • #error_log logs/error.log info;

重载Nginx配置

  1. /usr/local/Nginx/Nginx -s reload

PHP虚拟主机配置(支持PHP

解析域名并测试访问

http://www.example.com/index.html 有效

http://www.example.com/index.PHP 错误(下载资源文件

显然是我们的虚拟主机没有对PHP文件进行加载执行

给虚拟主机文件增加配置,如下:

  1. log_format soshash.log.format '$remote_addr - $remote_user [$time_local] $request'
  2. '$status $body_bytes_sent $http_referer '
  3. '$http_user_agent $http_x_forwarded_for';
  4. server {
  5. listen 80;
  6. server_name example.com www.example.com *.demo.example.com;
  7. index index.html index.htm index.PHP;
  8. root /mnt/web/example/wwwroot;
  9.  
  10. #新增配置如下
  11. location ~ .*\.(PHP|PHP5)?$ {
  12. fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.PHP;
  14. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  15. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  16. include fastcgi_params;
  17. }
  18. access_log /mnt/web/example/log/access.log example.log.format;
  19. error_log /mnt/web/example/log/error.log;
  20. }

重载Nginx

  1. /usr/local/Nginx/Nginx -s reload

再次测试通过。

类同,配置其他多个虚拟主机也一样如此简单。

多版本PHP简单说明

对于多版本PHP的话,只需要将其他PHP编译安装到另外一个目录,配置网站时监听对应的端口即可。

如:/usr/local/PHP/PHP7/

修改配置:PHP-fpm.conf

  1. listen = 127.0.0.1:9001

对应Nginx虚拟主机的配置更改

  1. location ~ .*\.(PHP|PHP5)?$ {
  2. fastcgi_pass 127.0.0.1:9001;#不同端口对应不同PHP版本
  3. fastcgi_index index.PHP;
  4. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  5. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  6. include fastcgi_params;
  7. }

异同之处只有这些,配置起来是比较简单的。

猜你在找的CentOS相关文章