debian apache
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计划的一部分。
介绍 (Introduction)
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 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.0. The second, site2.your_domain
, will deploy PHP 7.2.
在本教程中,您将在一个实例上设置两个PHP网站。 每个站点都将使用其自己的域,并且每个域都将部署其自己PHP版本。 第一个site1.your_domain
将部署PHP 7.0。 第二个site2.your_domain
将部署PHP 7.2。
先决条件 (Prerequisites)
One Debian 10 server with at least 1GB of RAM set up by following the Initial Server Setup with Debian 10, including a sudo non-root user and a firewall.
通过使用Debian 10进行初始服务器设置来设置一台至少具有1GB RAM的Debian 10服务器,包括sudo非root用户和防火墙。
An Apache web server set up and configured by following How to Install the Apache Web Server on Debian 10.
通过遵循如何在Debian 10上安装 Apache Web服务器来设置和配置Apache Web服务器 。
A domain name configured to point to your Debian 10 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
andsite2.your_domain
.配置为指向您的Debian 10服务器的域名。 通过遵循如何从通用域注册商指向DigitalOcean域名服务器,您可以学习如何将域指向DigitalOcean Droplet。 就本教程而言,我们将使用两个子域,每个子域在DNS设置中均指定为
A
记录:site1.your_domain
和site2.your_domain
。
第1步-使用PHP-FPM安装PHP 7.0和7.2版 (Step 1 — Installing PHP Versions 7.0 and 7.2 with PHP-FPM)
With the prerequisites completed, you will now install PHP versions 7.0 and 7.2, as well as PHP-FPM and several additional extensions. But to accomplish this, you will first need to add the sury php
repository to your system.
完成前提条件后,您现在将安装PHP版本7.0和7.2,以及PHP-FPM和一些其他扩展。 但是要实现此目的,您首先需要将sury php
存储库添加到系统中。
First install several required packages including curl
, wget
, and gnupg2
:
首先安装几个必需的软件包,包括curl
, wget
和gnupg2
:
- sudo apt-get install curl wget gnupg2 ca-certificates lsb-release apt-transport-https -y sudo apt-get install curl wget gnupg2 ca证书lsb-release apt-transport-https -y
The above packages will allow you to access the sury php
repository, and to do so securely. sury php
is a third-party repository or PPA (personal package archive). It offers PHP 7.4, 7.3, 7.2, 7.1, and 7.0 for the Debian operating system. It also offers more up-to-date versions of PHP than the official Debian 10 repositories, and will allow you to install multiple versions of PHP on the same system.
上述软件包将使您能够安全地访问sury php
存储库。 sury php
是第三方存储库或PPA(个人软件包存档)。 它为Debian操作系统提供PHP 7.4、7.3、7.2、7.1和7.0。 它还提供了比Debian 10官方存储库更多的最新PHP版本,并且允许您在同一系统上安装多个PHP版本。
Next, import the package’s key:
接下来,导入包的密钥:
- wget https://packages.sury.org/php/apt.gpg wget https://packages.sury.org/php/apt.gpg
- sudo apt-key add apt.gpg sudo apt-key添加apt.gpg
Now add the sury php
repository to your system:
现在将sury php
存储库添加到您的系统中:
- echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.list 回声“ deb https://packages.sury.org/php/ $(lsb_release -sc)main” | sudo tee /etc/apt/sources.list.d/php7.list
Update the repository:
更新存储库:
- sudo apt-get update -y sudo apt-get更新-y
Next, install php7.0
, php7.0-fpm
, php7.0-mysql
, libapache2-mod-php7.0
, and libapache2-mod-fcgid
with the following commands:
接下来,使用以下命令安装php7.0
, php7.0-fpm
, php7.0-mysql
, libapache2-mod-php7.0
和libapache2-mod-fcgid
:
- sudo apt-get install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y 须藤apt-get install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y
php7.0
is a metapackage that can be used to run PHP applications.php7.0
是可用于运行PHP应用程序的元包。php7.0-fpm
provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.php7.0-fpm
提供了Fast Process Manager解释器,该解释器作为守护程序运行并接收Fast / CGI请求。php7.0-mysql
connects PHP to the MySQL database.php7.0-mysql
将PHP连接到MySQL数据库。libapahce2-mod-php7.0
provides the PHP module for the Apache webserver.libapahce2-mod-php7.0
为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.2. Install php7.2
, php7.2-fpm
, php7.2-mysql
, and libapache2-mod-php7.2
.
现在,对PHP 7.2版重复该过程。 安装php7.2
, php7.2-fpm
, php7.2-mysql
和libapache2-mod-php7.2
。
- sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y 须藤apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y
After installing both PHP versions, start the php7.0-fpm
service:
安装两个PHP版本之后,启动php7.0-fpm
服务:
- sudo systemctl start php7.0-fpm sudo systemctl启动php7.0-fpm
Next, verify the status of the php7.0-fpm
service:
接下来,验证php7.0-fpm
服务的状态:
- sudo systemctl status php7.0-fpm sudo systemctl状态php7.0-fpm
You’ll see the following output:
您将看到以下输出:
Output
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-04-04 08:51:47 UTC; 1min 17s ago
Docs: man:php-fpm7.0(8)
Main PID: 13016 (php-fpm7.0)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 1149)
Memory: 19.1M
CGroup: /system.slice/php7.0-fpm.service
├─13016 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
├─13017 php-fpm: pool www
└─13018 php-fpm: pool www
Apr 04 08:51:47 debian10 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Apr 04 08:51:47 debian10 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.
Repeating this process, now start the php7.2-fpm
service:
重复此过程,现在启动php7.2-fpm
服务:
- sudo systemctl start php7.2-fpm sudo systemctl启动php7.2-fpm
And then verify the status of the php7.2-fpm
service:
然后验证php7.2-fpm
服务的状态:
- sudo systemctl status php7.2-fpm sudo systemctl状态php7.2-fpm
You’ll see the following output:
您将看到以下输出:
Output
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-04-04 08:52:52 UTC; 1min 32s ago
Docs: man:php-fpm7.2(8)
Process: 22207 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.2/fpm/pool.d/www.conf 72 (code=exite
Main PID: 22204 (php-fpm7.2)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 1149)
Memory: 12.0M
CGroup: /system.slice/php7.2-fpm.service
├─22204 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
├─22205 php-fpm: pool www
└─22206 php-fpm: pool www
Apr 04 08:52:52 debian10 systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
Apr 04 08:52:52 debian10 systemd[1]: Started The PHP 7.2 FastCGI Process Manager.
Lastly, you must enable several modules so that your Apache2 service can work with multiple PHP versions:
最后,您必须启用几个模块,以便您的Apache2服务可以与多个PHP版本一起使用:
- sudo a2enmod actions fcgid alias proxy_fcgi sudo a2enmod操作fcgid别名proxy_fcgi
actions
is used for executing CGI scripts based on media type or request method.actions
用于根据媒体类型或请求方法执行CGI脚本。fcgid
is a high performance alternative tomod_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服务以应用您的更改:
- sudo systemctl restart apache2 sudo systemctl重新启动apache2
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版本。 接下来,您将为要部署的每个网站创建目录结构。
第2步-为两个网站创建目录结构 (Step 2 — Creating Directory Structures for Both Websites)
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:
添加以下行:
<?php phpinfo(); ?>
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版本。
步骤3 —为两个网站配置Apache (Step 3 — Configuring Apache for Both Websites)
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.0
:
首先为网站site1.your_domain
创建一个新的虚拟主机配置文件。 在这里,您将指导Apache使用php7.0
渲染内容:
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版本与您的设置匹配:
<VirtualHost *:80>
ServerAdmin admin@site1.your_domain
ServerName site1.your_domain
DocumentRoot /var/www/site1.your_domain
DirectoryIndex info.php
<Directory /var/www/site1.your_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<FilesMatch \.php$>
# For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>
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.2
:
接下来,为网站site2.your_domain
创建一个新的虚拟主机配置文件。 您将指定此子域来部署php7.2
:
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版本与您的唯一信息匹配:
<VirtualHost *:80>
ServerAdmin admin@site2.your_domain
ServerName site2.your_domain
DocumentRoot /var/www/site2.your_domain
DirectoryIndex info.php
<Directory /var/www/site2.your_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<FilesMatch \.php$>
# For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>
Save and close the file when you are finished. Then check the Apache configuration file for any syntax errors:
完成后保存并关闭文件。 然后检查Apache配置文件是否存在语法错误:
- sudo apachectl configtest 须藤apachectl configtest
You’ll see the following output:
您将看到以下输出:
Output
Syntax OK
Next, enable both virtual host configuration files:
接下来,启用两个虚拟主机配置文件:
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.:
现在禁用默认站点,因为您将不需要它。:
- sudo a2dissite 000-default.conf 须藤a2dissite 000-default.conf
Finally, restart the Apache service to implement your changes:
最后,重新启动Apache服务以实现您的更改:
- sudo systemctl restart apache2 sudo systemctl重新启动apache2
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版本。
第4步-测试两个网站 (Step 4 — Testing Both Websites)
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.0. The second indicates that site2.your_domain
deployed PHP version 7.2.
注意标题。 第一页表明site1.your_domain
部署了PHP 7.0版。 第二个指示site2.your_domain
部署了PHP版本7.2。
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 threat. 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 Debian 10 server handling two websites with two different PHP versions. PHP-FPM, however, is not limited to this one application.
现在,您有一台Debian 10服务器,可以处理具有两个不同PHP版本的两个网站。 但是,PHP-FPM不限于此一个应用程序。
结论 (Conclusion)
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证书保护网站的安全。
debian apache