The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.
作者选择了COVID-19救济基金来接受捐赠,这是Write for DOnations计划的一部分。
The Apache web server uses virtual hosts to manage multiple domains on a single instance. Similarly, PHP-FPM uses a daemon to manage multiple PHP versions on a single instance. Together, you can use Apache and PHP-FPM to host multiple PHP web-applications, each using a different version of PHP, all on the same server, and all at the same time. This is useful because different applications may require different versions of PHP, but some server stacks, like a regularly configured LAMP stack, can only manage one. Combining Apache with PHP-FPM is also a more cost-efficient solution than hosting each application on its own instance.
Apache Web服务器使用虚拟主机来管理单个实例上的多个域。 同样, PHP-FPM使用守护程序在单个实例上管理多个PHP版本。 您可以一起使用Apache和PHP-FPM托管多个PHP Web应用程序,每个Web应用程序都使用不同版本PHP,并且全部都在同一服务器上,并且同时全部。 这很有用,因为不同的应用程序可能需要不同版本PHP,但是某些服务器堆栈( 如定期配置的LAMP堆栈 )只能管理一个。 与将每个应用程序托管在自己的实例上相比,将Apache与PHP-FPM结合使用也是一种更具成本效益的解决方案。
PHP-FPM also offers configuration options for stderr
and stdout
logging, emergency restarts, and adaptive process spawning, which is useful for heavy-loaded sites. In fact, using Apache with PHP-FPM is one of the best stacks for hosting PHP applications, especially when it comes to performance.
PHP-FPM还提供了用于stderr
和stdout
日志记录,紧急重启和自适应进程生成的配置选项,这对于负载较重的站点非常有用。 实际上,将Apache与PHP-FPM结合使用是托管PHP应用程序的最佳堆栈之一,尤其是在性能方面。
In fact, using Apache with PHP-FPM is one of the best stacks for hosting PHP applications, especially when it comes to performance. PHP-FPM not only allows you run to multiple PHP versions simultaneously, it also provides numerous extra features like adaptive process spawning, which is useful for heavy-loaded sites.
实际上,将Apache与PHP-FPM结合使用是托管PHP应用程序的最佳堆栈之一,尤其是在性能方面。 PHP-FPM不仅允许您同时运行多个PHP版本,而且还提供了许多其他功能,例如自适应进程生成,这对于负载较重的站点非常有用。
In this tutorial you will set up two PHP sites on a single instance. Each site will use its own domain, and each domain will deploy its own version of PHP. The first, site1.your_domain
, will deploy PHP 7.2. The second, site2.your_domain
, will deploy PHP 7.3.
在本教程中,您将在一个实例上设置两个PHP网站。 每个站点都将使用其自己的域,并且每个域都将部署其自己PHP版本。 第一个site1.your_domain
将部署PHP 7.2。 第二个site2.your_domain
将部署PHP 7.3。
One Ubuntu 20.04 server with at least 1GB of RAM set up by following the Initial Server Setup with Ubuntu 20.04, including a sudo non-root user and a firewall.
通过使用Ubuntu 20.04进行初始服务器设置来设置一台至少具有1GB RAM的Ubuntu 20.04服务器,包括sudo非root用户和防火墙。
An Apache web server set up and configured by following How to Install the Apache Web Server on Ubuntu 20.04.
通过遵循如何在Ubuntu 20.04上安装 Apache Web服务器来设置和配置Apache Web服务器 。
A domain name configured to point to your Ubuntu 20.04 server. You can learn how to point domains to DigitalOcean Droplets by following How To Point to DigitalOcean Nameservers From Common Domain Registrars. For the purposes of this tutorial, we will use two subdomains, each specified with an A
record in our DNS settings: site1.your_domain
and site2.your_domain
.
配置为指向您的Ubuntu 20.04服务器的域名。 通过遵循如何从通用域注册商指向DigitalOcean域名服务器,您可以学习如何将域指向DigitalOcean Droplet。 就本教程而言,我们将使用两个子域,每个子域在DNS设置中均指定为A
记录: site1.your_domain
和site2.your_domain
。
With the prerequisites completed, you will now install PHP versions 7.2 and 7.3, as well as PHP-FPM and several additional extensions. But to accomplish this, you will first need to add the Ondrej PHP
repository to your system.
完成前提条件后,您现在将安装PHP版本7.2和7.3,以及PHP-FPM和一些其他扩展。 但是要实现此目的,您首先需要将Ondrej PHP
存储库添加到系统中。
Execute the apt-get
command to install software-properties-common
:
执行apt-get
命令以安装software-properties-common
:
The software-properties-common
package provides apt-add-repository
command-line utility which you will use to add the ondrej/php
PPA (Personal Package Archive) repository.
software-properties-common
软件包提供了apt-add-repository
命令行实用程序,您可以使用该实用程序添加ondrej/php
PPA(个人软件包存档)存储库。
Now add the ondrej/php
repository to your system. The ondrej/php
PPA will have more up-to-date versions of PHP than the official Ubuntu repositories, and it will also allow you to install multiple versions of PHP in the same system:
现在将ondrej/php
存储库添加到您的系统。 与正式的Ubuntu存储库相比, ondrej/php
PPA将具有更多最新版本PHP,并且还允许您在同一系统中安装多个PHP版本:
Update the repository:
更新存储库:
Next, install php7.2
, php7.2-fpm
, php7.2-mysql
, libapache2-mod-php7.2
, and libapache2-mod-fcgid
with the following commands:
接下来,使用以下命令安装php7.2
, php7.2-fpm
, php7.2-mysql
, libapache2-mod-php7.2
和libapache2-mod-fcgid
:
php7.2
is a metapackage used to run PHP applications.
php7.2
是用于运行PHP应用程序的元php7.2
。
php7.2-fpm
provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
php7.2-fpm
提供了作为守护程序运行的Fast Process Manager解释器,并接收Fast / CGI请求。
php7.2-mysql
connects PHP to the MySQL database.
php7.2-mysql
将PHP连接到MySQL数据库。
libapache2-mod-php7.2
provides the PHP module for the Apache webserver.
libapache2-mod-php7.2
为Apache Web服务器提供了PHP模块。
libapache2-mod-fcgid
contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.
libapache2-mod-fcgid
包含一个mod_fcgid,它启动许多CGI程序实例来处理并发请求。
Now repeat the process for PHP version 7.3. Install php7.3
, php7.3-fpm
, php7.3-mysql
, and libapache2-mod-php7.3
.
现在,为PHP 7.3重复该过程。 安装php7.3
, php7.3-fpm
, php7.3-mysql
和libapache2-mod-php7.3
。
After installing both PHP versions, start the php7.2-fpm
service:
安装两个PHP版本之后,启动php7.2-fpm
服务:
Next, verify the status of php7.2-fpm
service:
接下来,验证php7.2-fpm
服务的状态:
You’ll see the following output:
您将看到以下输出:
Active: active (running) since Fri 2020-06-05 11:25:07 UTC; 1min 38s ago
活动:自星期五2020-06-05 11:25:07 UTC起活动 (运行); 1min 38s前
Repeating this process, now start the php7.3-fpm
service:
重复此过程,现在启动php7.3-fpm
服务:
Next, verify the status of php7.3-fpm
service:
接下来,验证php7.3-fpm
服务的状态:
You’ll see the following output:
您将看到以下输出:
Active: active (running) since Fri 2020-06-05 11:26:33 UTC; 56s ago
活动:自星期五2020-06-05 11:26:33 UTC起活动 (运行); 56s前
Lastly, you must enable several modules so that your Apache2 service can work with multiple PHP versions:
最后,您必须启用几个模块,以便您的Apache2服务可以与多个PHP版本一起使用:
actions
is used for executing CGI scripts based on media type or request method.
actions
用于根据媒体类型或请求方法执行CGI脚本。
fcgid
is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
fcgid
是mod_cgi的高性能替代方案,它启动了足够数量的CGI程序实例来处理并发请求。
alias
provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
alias
提供了文件树中主机文件系统不同部分的映射以及URL重定向。
proxy_fcgi
allows Apache to forward requests to PHP-FPM.
proxy_fcgi
允许Apache将请求转发到PHP-FPM。
Now restart the Apache service to apply your changes:
现在,重新启动Apache服务以应用您的更改:
At this point you have installed two PHP versions on your server. Next, you will create a directory structure for each website you want to deploy.
至此,您已经在服务器上安装了两个PHP版本。 接下来,您将为要部署的每个网站创建目录结构。
In this section, you will create a document root directory and an index page for each of your two websites.
在本部分中,您将为两个网站中的每个网站创建一个文档根目录和一个索引页。
First, create document root directories for both site1.your_domain
and site2.your_domain
:
首先,为site1.your_domain
和site2.your_domain
创建文档根目录:
sudo mkdir /var/www/site1.your_domain
须藤mkdir / var / www / site1.your_domain
sudo mkdir /var/www/site2.your_domain
须藤mkdir / var / www / site2.your_domain
By default, the Apache webserver runs as a www-data user and www-data group. To ensure that you have the correct ownership and permissions of your website root directories, execute the following commands:
默认情况下,Apache Web服务器以www-data用户和www-data组的身份运行。 为确保您具有网站根目录的正确所有权和权限,请执行以下命令:
sudo chown -R www-data:www-data /var/www/site1.your_domain
须藤chown -R www-data:www-data / var / www / site1.your_domain
sudo chown -R www-data:www-data /var/www/site2.your_domain
须藤chown -R www-data:www-data / var / www / site2.your_domain
sudo chmod -R 755 /var/www/site1.your_domain
须藤chmod -R 755 / var / www / site1.your_domain
sudo chmod -R 755 /var/www/site2.your_domain
须藤chmod -R 755 / var / www / site2.your_domain
Next you will create an info.php
file inside each website root directory. This will display each website’s PHP version information. Begin with site1:
接下来,您将在每个网站根目录中创建一个info.php
文件。 这将显示每个网站PHP版本信息。 从site1开始:
sudo nano /var/www/site1.your_domain/info.php
须藤纳米/ var / www / site1.your_domain /info.php
Add the following line:
添加以下行:
Save and close the file. Now copy the info.php file you created to site2:
保存并关闭文件。 现在,将您创建的info.php文件复制到site2 :
sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php
须藤cp / var / www / site1.your_domain /info.php / var / www / site2.your_domain /info.php
Your web server should now have the document root directories that each site requires to serve data to visitors. Next, you will configure your Apache web server to work with two different PHP versions.
您的Web服务器现在应该具有每个站点向访问者提供数据所需的文档根目录。 接下来,您将配置Apache Web服务器以使用两个不同PHP版本。
In this section, you will create two virtual host configuration files. This will enable your two websites to work simultaneously with two different PHP versions.
在本节中,您将创建两个虚拟主机配置文件。 这将使您的两个网站可以同时使用两个不同PHP版本。
In order for Apache to serve this content, it is necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf
, you’ll create two new ones inside the directory /etc/apache2/sites-available/
.
为了使Apache能够提供此内容,必须使用正确的指令创建虚拟主机文件。 无需修改/etc/apache2/sites-available/000-default.conf
的默认配置文件,而是在目录/etc/apache2/sites-available/
创建两个新配置文件。
First create a new virtual host configuration file for the website site1.your_domain. Here you will direct Apache to render content using php7.2
:
首先为网站site1.your_domain创建一个新的虚拟主机配置文件。 在这里,您将指导Apache使用php7.2
呈现内容:
sudo nano /etc/apache2/sites-available/site1.your_domain.conf
须藤纳米/ etc / apache2 / sites-available / site1.your_domain .conf
Add the following content. Make sure the website directory path, server name, and PHP version match your setup:
添加以下内容。 确保网站目录路径,服务器名称和PHP版本与您的设置匹配:
ServerAdmin [email protected]_domain
ServerName site1.your_domain
DocumentRoot /var/www/site1.your_domain
DirectoryIndex info.php
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
In this file you updated the DocumentRoot to your new directory and ServerAdmin to an email that the your_domain site administrator can access. You’ve also updated ServerName, which establishes the base domain for this virtual host configuration, and you’ve added a SetHandler directive to run PHP as a fastCGI process server.
在此文件中,您将DocumentRoot更新为新目录,并将ServerAdmin更新为your_domain站点管理员可以访问的电子邮件。 您还更新了ServerName ,它为该虚拟主机配置建立了基础域,并添加了SetHandler指令以将PHP作为fastCGI进程服务器运行。
Save and close the file.
保存并关闭文件。
Next, create a new virtual host configuration file for the website site2.your_domain. You will specify this subdomain to deploy php7.3
:
接下来,为网站site2.your_domain创建一个新的虚拟主机配置文件。 您将指定此子域来部署php7.3
:
sudo nano /etc/apache2/sites-available/site2.your_domain.conf
须藤纳米/ etc / apache2 / sites-available / site2.your_domain .conf
Add the following content. Again, make sure the website directory path, server name, and PHP version match your unique information:
添加以下内容。 再次,确保网站目录路径,服务器名称和PHP版本与您的唯一信息匹配:
ServerAdmin [email protected]_domain
ServerName site2.your_domain
DocumentRoot /var/www/site2.your_domain
DirectoryIndex info.php
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
Save and close the file when you are finished. Then, check the Apache configuration file for any syntax errors:
完成后保存并关闭文件。 然后,检查Apache配置文件是否存在语法错误:
You’ll see the following output:
您将看到以下输出:
Next, enable both virtual host configuration files with the following commands:
接下来,使用以下命令启用两个虚拟主机配置文件:
sudo a2ensite site1.your_domain
sudo a2ensite site1.your_domain
sudo a2ensite site2.your_domain
sudo a2ensite site2.your_domain
Now disable the default site, since you won’t need it.:
现在禁用默认站点,因为您将不需要它。:
Finally, restart the Apache service to implement your changes:
最后,重新启动Apache服务以实现您的更改:
Now that you have configured Apache to serve each site, you will test them to make sure the proper PHP versions are running.
既然已经配置了Apache为每个站点提供服务,您将对其进行测试以确保正在运行正确PHP版本。
At this point, you have configured two websites to run two different versions of PHP. Now test the results.
至此,您已经配置了两个网站来运行两个不同版本PHP。 现在测试结果。
Open your web browser and visit both sites http://site1.your_domain
and http://site2.your_domain
. You will see two pages that look like this:
打开您的Web浏览器,并访问两个站点http:// site1.your_domain
和http:// site2.your_domain
。 您将看到两个页面,如下所示:
Note the titles. The first page indicates that site1.your_domain
deployed PHP version 7.2. The second indicates that site2.your_domain
deployed PHP version 7.3.
注意标题。 第一页表明site1.your_domain
部署了PHP版本7.2。 第二个指示site2.your_domain
部署了PHP版本7.3。
Now that you’ve tested your sites, remove the info.php
files. Because they contain sensitive information about your server and are accessible to unauthorized users, they pose a security vulnerability. To remove both files, run the following commands:
现在,您已经测试了站点,请删除info.php
文件。 因为它们包含有关服务器的敏感信息,并且未经授权的用户可以访问,所以它们构成了安全漏洞。 要删除这两个文件,请运行以下命令:
sudo rm -rf /var/www/site1.your_domain/info.php
须藤rm -rf / var / www / site1.your_domain /info.php
sudo rm -rf /var/www/site2.your_domain/info.php
须藤rm -rf / var / www / site2.your_domain /info.php
You now have a single Ubuntu 20.04 server handling two websites with two different PHP versions. PHP-FPM, however, is not limited to this one application.
现在,您只有一台Ubuntu 20.04服务器,可以处理具有两个不同PHP版本的两个网站。 但是,PHP-FPM不限于此一个应用程序。
You have now combined virtual hosts and PHP-FPM to serve multiple websites and multiple versions of PHP on a single server. The only practical limit on the number of PHP sites and PHP versions that your Apache service can handle is the processing power of your instance.
现在,您已经将虚拟主机和PHP-FPM组合在一起,以在单个服务器上为多个网站和多个版本PHP提供服务。 Apache服务可以处理PHP站点和PHP版本数量的唯一实际限制是实例的处理能力。
From here you might consider exploring PHP-FPM’s more advanced features, like its adaptive spawning process or how it can log sdtout
and stderr
Alternatively, you could now secure your websites. To accomplish this, you can follow our tutorial on how to secure your sites with free TLS/SSL certificates from Let’s Encrypt.
从这里开始,您可能会考虑探索PHP-FPM的更高级功能 ,例如其自适应产卵过程或如何记录sdtout
和stderr
或者,您现在可以保护您的网站。 为此,您可以按照我们的教程进行, 该教程介绍了如何使用来自Let's Encrypt的免费TLS / SSL证书保护网站的安全。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-ubuntu-20-04