Centos7+Nginx+PHP Basic WEB Running Environment-Multi-Virtual Host Configuration

Keywords: Nginx PHP

Previous words: Manual Deployment of Centos7+Nginx+PHP Basic WEB Running Environment

Previously, we manually deployed the Nginx+PHP running environment on Centos7, but did not mention the deployment method of multi-site virtual hosts. This article will continue to record some deployment of multi-site Nginx virtual hosts and pay attention to details.

Nginx installation path: / usr/local/nginx

Nginx Master Configuration: / usr/local/nginx/nginx.conf

Default website directory: / usr/local/nginx/html

If your configuration is different from the current configuration, be careful to replace the path in this article.

 

Dead work

Create site directories and configuration directories

#Create site directories and log directories generated by the site
mkdir /mnt/web/example/wwwroot -p
mkdir /mnt/web/example/log -p

#Create a hosting configuration directory loaded by nginx
mkdir /usr/local/nginx/vhost

#Create default files
echo "<?php phpinfo();>" > /mnt/web/example/wwwroot/index.php
echo "hi example.com" > /mnt/web/example/wwwroot/index.html

#Setting permissions
chown -R php-fpm:www /mnt/web
chmod -R 775 /mnt/web

 

configuration file

Normal Virtual Host Configuration (PHP is not supported)

Add a website configuration

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

The configuration file is as follows

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;
}

Domain name binding (server_name):

  • Single domain name: server_name www.example.com
  • Multi-domain name: server_name www.example.com php.example.com
  • Pan-domain name: server_name*.demo.example.com
  • And regular matching domain names. Domain names can be bound to multiple domains, and only need to be separated by spaces.

Default file (index): Displays default pages in priority order.

Site directory (root): Fill in the site directory we created beforehand.

Access log file (access_log):

  • access_log generates a log file to store the path log content format (example.log.format)
  • example.log.format is equivalent to a variable and needs to be declared in advance.
  • The latest version of nginx (1.12.0) requires that log_format be placed outside the server segment, otherwise a similar error will be reported: nginx: [emerg] "log_format" directive is not allowed here in xxx.

Error log file (error_log):

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

Overloaded nginx configuration

/usr/local/nginx/nginx -s reload

 

PHP Virtual Host Configuration (PHP Support)

Resolve domain names and test access

http://www.example.com/index.html is valid

http://www.example.com/index.php error (download resource file)

Obviously, our virtual host did not load and execute PHP files.

Add configuration to the virtual host file as follows:

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;

        #The new configuration is as follows
	    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;
}

Heavy haul nginx

/usr/local/nginx/nginx -s reload

The test passed again.

Similarly, it is as simple to configure multiple other virtual hosts.

 

Simple description of multi-version PHP

For multi-version PHP, you just need to install other PHP compilers into another directory and listen on the corresponding ports when configuring the website.

For example: / usr/local/php/php7/

Modified configuration: php-fpm.conf

listen = 127.0.0.1:9001  

Configuration changes for nginx virtual hosts

	    location ~ .*\.(php|php5)?$ {
	    	fastcgi_pass 127.0.0.1:9001;#Different ports correspond to different php versions
		    fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
		    include fastcgi_params;
	    }

There are only these similarities and differences, which are relatively simple to configure.

Posted by beyzad on Fri, 14 Dec 2018 17:21:03 -0800