IDC知識(shí)庫
IDC領(lǐng)域?qū)I(yè)知識(shí)百科平臺(tái)

PHP虛擬主機(jī)用配置環(huán)境嗎

PHP虛擬主機(jī)用配置環(huán)境嗎。

我們?cè)贑entos7上手工部署了Nginx+PHP的運(yùn)行環(huán)境,然而并沒有說到虛擬主機(jī)多個(gè)站點(diǎn)的部署方法,此文將繼續(xù)記錄Nginx虛擬主機(jī)多站點(diǎn)的一些部署及注意細(xì)節(jié)。

Nginx安裝路徑:/usr/local/nginx

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

默認(rèn)網(wǎng)站目錄:/usr/local/nginx/html

如果您的配置和當(dāng)前的配置不一樣,注意將文中路徑替換。

準(zhǔn)備工作

創(chuàng)建網(wǎng)站目錄以及配置目錄

#創(chuàng)建網(wǎng)站目錄及網(wǎng)站產(chǎn)生的日志存放目錄
mkdir /mnt/web/example/wwwroot -p
mkdir /mnt/web/example/log -p

#創(chuàng)建 nginx 加載的虛擬主機(jī)配置存放目錄
mkdir /usr/local/nginx/vhost

#增加配置文件引入
vi /usr/local/nginx/nginx.conf
#在 http 段尾部增加 include /usr/local/nginx/vhost/*.conf;

#創(chuàng)建默認(rèn)文件
echo "<?php phpinfo();>" > /mnt/web/example/wwwroot/index.php
echo "hi example.com" > /mnt/web/example/wwwroot/index.html

#設(shè)置權(quán)限
chown -R php-fpm:www /mnt/web
chmod -R 775 /mnt/web

配置文件

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

增加一個(gè)網(wǎng)站配置

cd /usr/local/nginx/vhost
vi example.conf

配置文件內(nèi)容如下

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):

  • 單域名:server_name www.example.com
  • 多域名:server_name www.example.com php.example.com
  • 泛域名:server_name *.demo.example.com
  • 以及正則匹配域名。域名可以綁定多個(gè),只需要用空格分割開即可。

默認(rèn)文件(index):按照優(yōu)先順序顯示默認(rèn)網(wǎng)頁。

網(wǎng)站目錄(root):填寫我們預(yù)先創(chuàng)建的網(wǎng)站目錄。

訪問日志文件(access_log):

  • access_log 產(chǎn)生日志文件存儲(chǔ)路徑 日志內(nèi)容的格式(example.log.format)
  • example.log.format 相當(dāng)于變量一樣,需要提前聲明。
  • 最新版本的 nginx(1.12.0)需要 將log_format 放置到 server段外部,否則會(huì)報(bào)一個(gè)類似:nginx: [emerg] “log_format” directive is not allowed here in xxx 的錯(cuò)誤。

錯(cuò)誤日志文件(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虛擬主機(jī)配置(支持PHP)

解析域名并測(cè)試訪問

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

http://www.example.com/index.php ?錯(cuò)誤(下載資源文件)

顯然是我們的虛擬主機(jī)沒有對(duì)PHP文件進(jìn)行加載執(zhí)行

給虛擬主機(jī)文件增加配置,如下:

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

再次測(cè)試通過。

類同,配置其他多個(gè)虛擬主機(jī)也一樣如此簡(jiǎn)單。

多版本PHP簡(jiǎn)單說明

對(duì)于多版本PHP的話,只需要將其他PHP編譯安裝到另外一個(gè)目錄,配置網(wǎng)站時(shí)監(jiān)聽對(duì)應(yīng)的端口即可。

如:/usr/local/php/php7/

修改配置:php-fpm.conf

listen = 127.0.0.1:9001

對(duì)應(yīng)nginx虛擬主機(jī)的配置更改

	    location ~ .*\.(php|php5)?$ {
	    	fastcgi_pass 127.0.0.1:9001;#不同端口對(duì)應(yīng)不同php版本
		    fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
		    include fastcgi_params;
	    }

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

浜戣櫄鎷熶富鏈?
在哪里租用的虛擬主機(jī)和網(wǎng)站空間,就要在哪里進(jìn)行網(wǎng)站備案,網(wǎng)站備案需要提供完整真實(shí)的資料,否則無法通過,推薦來域名頻道,一站式服務(wù)。
所有的虛擬主機(jī)都自帶強(qiáng)大的管理面板,即使你不懂程序,一般情況下也能正常使用。
虛擬主機(jī)是基于最新的容器技術(shù)、熱遷移技術(shù)和百度生態(tài)能力提供的新一代網(wǎng)站主機(jī)服務(wù)。集高性能、高可靠性、高安全性和高易用性于。
推薦美國(guó)虛擬主機(jī)服務(wù)商:域名頻道http://nrfpj.cn/webhost/vhost_usa.asp

贊(2)
分享到: 更多 (0)

中國(guó)專業(yè)的網(wǎng)站域名及網(wǎng)站空間提供商

買域名買空間