IDC知识库
IDC领域专业知识百科平台

PHP虚拟主机用配置环境吗

PHP虚拟主机用配置环境吗。

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

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

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

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

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

准备工作

建网站目录以及配置目录

#创建网站目录及网站产生的日志存放目录
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):

  • 单域名: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;
	    }

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

浜戣櫄鎷熶富鏈�
在哪里租用的虚拟主机和网站空间,就要在哪里进行网站备案,网站备案需要提供完整真实的资料,否则无法通过,推荐来域名频道,一站式服务。
所有的虚拟主机都自带强大的管理面板,即使你不懂程序,一般情况下也能正常使用。
虚拟主机是基于最新的容器技术、热迁移技术和百度生态能力提供的新一代网站主机服务。集高性能、高可靠性、高安全性和高易用性于。
推荐美国虚拟主机服务商:域名频道http://www.dns110.com/webhost/vhost_usa.asp

赞(3)
分享到: 更多 (0)

中国专业的网站域名及网站空间提供商

买域名买空间