CentOS7 下搭建 lemp

本文只做原文备份和本地化记录,原文请直接访问:How to Run Multiple Websites with Different PHP Versions in Nginx


  • Nginx HTTP Server.
  • Nginx uses PHP-FPM (stands for FastCGI Process Manager).
  • PHP 7.1 (to be used as default version) and 5.6.
  • MariaDB Database Server.
  • Server IP address:
  • Websites: example1.com and example2.com.

Step 1: Installing and Enabling EPEL and Remi Repository

  1. First start by installing and enabling the EPEL and Remi repository, which offers the latest versions of the PHP stack on CentOS/RHEL 7 distributions.

Command to install the EPEL repository configuration package:

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Command to install the Remi repository configuration package:

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Command to install the yum-utils package (for the yum-config-manager command):

yum install yum-utils

yum-config-manager is used to enable or disable Yum repositories on the system.

Step 2: Installing Nginx Web Server

yum install nginx

Step 3: Installing MariaDB Database Server

Step 4: Installing Multiple Versions of PHP

  1. To install different versions of PHP for your projects, use yum-config-manager command to install multiple versions of PHP along with most required modules as shown.

Install PHP 7.1 Version

Command to enable the repository:

yum-config-manager --enable remi-php71

Command to upgrade (the repository only provides PHP):

yum update

Command to install default version PHP 7.1:

yum install php php-common php-fpm

校验默认的 PHP7.1:

php --ini

Install PHP 5.6 Version

yum install php56 php56-php-common php56-php-fpm

校验 PHP5.6:

php56 --ini

Step 5: Configuring PHP-FPM and PHP56-PHP-FPM

  1. This is the most interesting part of this tutorial, it explains how you can actually run multiple PHP versions on your server. Here, you will configure the different versions of php-fpm that Nginx will work with. You should define the user/group of the FastCGI processes as well as the ports they will listen on.

These are the following two configuration files that you will going to edit.

  • php-fpm(default 7.1) - /etc/php-fpm.d/www.conf
  • php56-php-fpm - /etc/opt/remi/php56/php-fpm.d/www.conf

Open the files above, set the user/group of FastCGI processes.

vi /etc/php-fpm.d/www.conf   [PHP 7.1]
vi /etc/opt/remi/php56/php-fpm.d/www.conf  [PHP 5.6]

The default values should be apache, change them to nginx as shown.

user = nginx
group = nginx
  1. Next, find the listen parameters, and define the address:port on which FastCGI requests will be received.
listen =	[php-fpm]
listen =	[php56-php-fpm]
  1. Once all the above configuration done, you need to start and enable Nginx, MariaDB and PHP-FPM to auto-start at system boot.
systemctl enable nginx 
systemctl start nginx 
systemctl enable mariadb 
systemctl start mariadb 
---------------- PHP 7.1 ---------------- 
systemctl enable php-fpm 
systemctl start php-fpm 
---------------- PHP 5.6 ----------------
systemctl enable php56-fpm 
systemctl start php56-php-fpm 
:In case you get any errors while starting the second instance of PHP, `php56-php-fpm`, a SELinux policy could be blocking it from starting. If SELinux is in `enforcing mode`, set it to `permissive` mode, then try starting the service once again.
setenforce 0 

Step 6: Setup Websites with Permissions

  1. At this point, you can now create the necessary directories for your websites under /var/www/html/. You also need to create directories to store logs as follows:
---------------- Website 1 ----------------
mkdir -p /var/www/html/example1.com/ 
mkdir -p /var/log/nginx/example1.com/ 
---------------- Website 2 ----------------
mkdir -p /var/www/html/example2.com/
mkdir -p /var/log/nginx/example2.com/ 
  1. Set the appropriate ownership permissions on all the directories.
---------------- Website 1 ----------------
# chown -R root:nginx /var/www/html/example1.com/ 
# chmod -R 755 /var/www/html/example1.com/ 
# chown -R root:nginx /var/log/nginx/example1.com/
# chmod -R 660 /var/log/nginx/example1.com/ 
---------------- Website 2 ----------------
# chown -R root:nginx /var/www/html/example2.com/ 
# chmod -R 755 /var/www/html/example2.com/
# chown -R root:nginx /var/log/nginx/example2.com/ 
# chmod -R 660 /var/log/nginx/example2.com/

Step 7: Setup Nginx Server Blocks for Websites

Step 8: Testing Different PHP Versions

Ubuntu16 下搭建 lemp