Centos7+Nginx+PHP 基礎WEB運行環境-多虛擬主機配置

前文: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

 

配置文件

普通虛擬主機配置(不支持PHP)

增長一個網站配置測試

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

  • 單域名: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配置

/usr/local/nginx/nginx -s reload

 

PHP虛擬主機配置(支持PHP)

解析域名並測試訪問

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的話,只須要將其餘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;
	    }

異同之處只有這些,配置起來是比較簡單的。

相關文章
相關標籤/搜索