从 Apache2 迁移到 Nginx Web 服务器
最近,我不得不将公司开发、测试和生产环境的 Web 服务器从 Apache2 迁移到 Nginx。迁移的原因是,公司其他部门的后端服务都使用 Nginx,但在搭建网站时,由于某种原因,顾问们设置了 Apache2。这给管理层带来了很大困扰。在本文中,我将逐步讲解整个迁移过程。所有截图均使用 Vagrant Box 进行,但实际服务器的操作步骤与此相同。
服务器
我的测试服务器使用的是vagrant
和ubuntu/bionic64
。在这台服务器上,我之前有apache2.4
、php7.2
和mysql5.7
一个基础LAMP
堆栈。我将把它迁移到LEMP
用 Nginx 替换 Apache 的堆栈。
这台服务器上运行着一个 WordPress 网站,版本号为 WordPress 5.2.4。一旦我们能看到这个 WordPress 网站在 Nginx 和 PHP 7.3 上运行,我们的目标就达成了。对了,我们envvars
还需要将 Apache2 实例的一系列设置迁移到 Nginx 上。
安装 Nginx
非常简单。首先检查更新。
sudo apt update
sudo apt upgrade
实际安装 Nginx。
sudo apt install nginx
Nginx 现在已经安装完毕,但尚未运行。由于我们已经在系统上安装并配置了 Apache2,Apache2 将使用端口 ,80
这意味着为了避免冲突,我们应该在不同的端口上运行 Nginx,以测试一切是否运行正常。我选择了 端口8080
。
使用端口 8080 配置 Nginx
为了确保一切正常,我们默认使用 Nginx 设置了一个网站(与 Apache 类似)。您可以在此处查看配置。
sudo vim /etc/nginx/sites-available/default
这应该会打开一个如下所示的文件。
## | |
# You should look at the following URL's in order to grasp a solid understanding | |
# of Nginx configuration files in order to fully unleash the power of Nginx. | |
# https://www.nginx.com/resources/wiki/start/ | |
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ | |
# https://wiki.debian.org/Nginx/DirectoryStructure | |
# | |
# In most cases, administrators will remove this file from sites-enabled/ and | |
# leave it as reference inside of sites-available where it will continue to be | |
# updated by the nginx packaging team. | |
# | |
# This file will automatically load configuration files provided by other | |
# applications, such as Drupal or Wordpress. These applications will be made | |
# available underneath a path with that package name, such as /drupal8. | |
# | |
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. | |
## | |
# Default server configuration | |
# | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
# SSL configuration | |
# | |
# listen 443 ssl default_server; | |
# listen [::]:443 ssl default_server; | |
# | |
# Note: You should disable gzip for SSL traffic. | |
# See: https://bugs.debian.org/773332 | |
# | |
# Read up on ssl_ciphers to ensure a secure configuration. | |
# See: https://bugs.debian.org/765782 | |
# | |
# Self signed certs generated by the ssl-cert package | |
# Don't use them in a production server! | |
# | |
# include snippets/snakeoil.conf; | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files $uri $uri/ =404; | |
} | |
# pass PHP scripts to FastCGI server | |
# | |
#location ~ \.php$ { | |
# include snippets/fastcgi-php.conf; | |
# | |
# # With php-fpm (or other unix sockets): | |
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; | |
# # With php-cgi (or other tcp sockets): | |
# fastcgi_pass 127.0.0.1:9000; | |
#} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
#location ~ /\.ht { | |
# deny all; | |
#} | |
} | |
# Virtual Host configuration for example.com | |
# | |
# You can move that to a different file under sites-available/ and symlink that | |
# to sites-enabled/ to enable it. | |
# | |
#server { | |
# listen 80; | |
# listen [::]:80; | |
# | |
# server_name example.com; | |
# | |
# root /var/www/example.com; | |
# index index.html; | |
# | |
# location / { | |
# try_files $uri $uri/ =404; | |
# } | |
#} |
## | |
# You should look at the following URL's in order to grasp a solid understanding | |
# of Nginx configuration files in order to fully unleash the power of Nginx. | |
# https://www.nginx.com/resources/wiki/start/ | |
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ | |
# https://wiki.debian.org/Nginx/DirectoryStructure | |
# | |
# In most cases, administrators will remove this file from sites-enabled/ and | |
# leave it as reference inside of sites-available where it will continue to be | |
# updated by the nginx packaging team. | |
# | |
# This file will automatically load configuration files provided by other | |
# applications, such as Drupal or Wordpress. These applications will be made | |
# available underneath a path with that package name, such as /drupal8. | |
# | |
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. | |
## | |
# Default server configuration | |
# | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
# SSL configuration | |
# | |
# listen 443 ssl default_server; | |
# listen [::]:443 ssl default_server; | |
# | |
# Note: You should disable gzip for SSL traffic. | |
# See: https://bugs.debian.org/773332 | |
# | |
# Read up on ssl_ciphers to ensure a secure configuration. | |
# See: https://bugs.debian.org/765782 | |
# | |
# Self signed certs generated by the ssl-cert package | |
# Don't use them in a production server! | |
# | |
# include snippets/snakeoil.conf; | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files $uri $uri/ =404; | |
} | |
# pass PHP scripts to FastCGI server | |
# | |
#location ~ \.php$ { | |
# include snippets/fastcgi-php.conf; | |
# | |
# # With php-fpm (or other unix sockets): | |
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; | |
# # With php-cgi (or other tcp sockets): | |
# fastcgi_pass 127.0.0.1:9000; | |
#} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
#location ~ /\.ht { | |
# deny all; | |
#} | |
} | |
# Virtual Host configuration for example.com | |
# | |
# You can move that to a different file under sites-available/ and symlink that | |
# to sites-enabled/ to enable it. | |
# | |
#server { | |
# listen 80; | |
# listen [::]:80; | |
# | |
# server_name example.com; | |
# | |
# root /var/www/example.com; | |
# index index.html; | |
# | |
# location / { | |
# try_files $uri $uri/ =404; | |
# } | |
#} |
## | |
# You should look at the following URL's in order to grasp a solid understanding | |
# of Nginx configuration files in order to fully unleash the power of Nginx. | |
# https://www.nginx.com/resources/wiki/start/ | |
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ | |
# https://wiki.debian.org/Nginx/DirectoryStructure | |
# | |
# In most cases, administrators will remove this file from sites-enabled/ and | |
# leave it as reference inside of sites-available where it will continue to be | |
# updated by the nginx packaging team. | |
# | |
# This file will automatically load configuration files provided by other | |
# applications, such as Drupal or Wordpress. These applications will be made | |
# available underneath a path with that package name, such as /drupal8. | |
# | |
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. | |
## | |
# Default server configuration | |
# | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
# SSL configuration | |
# | |
# listen 443 ssl default_server; | |
# listen [::]:443 ssl default_server; | |
# | |
# Note: You should disable gzip for SSL traffic. | |
# See: https://bugs.debian.org/773332 | |
# | |
# Read up on ssl_ciphers to ensure a secure configuration. | |
# See: https://bugs.debian.org/765782 | |
# | |
# Self signed certs generated by the ssl-cert package | |
# Don't use them in a production server! | |
# | |
# include snippets/snakeoil.conf; | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files $uri $uri/ =404; | |
} | |
# pass PHP scripts to FastCGI server | |
# | |
#location ~ \.php$ { | |
# include snippets/fastcgi-php.conf; | |
# | |
# # With php-fpm (or other unix sockets): | |
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; | |
# # With php-cgi (or other tcp sockets): | |
# fastcgi_pass 127.0.0.1:9000; | |
#} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
#location ~ /\.ht { | |
# deny all; | |
#} | |
} | |
# Virtual Host configuration for example.com | |
# | |
# You can move that to a different file under sites-available/ and symlink that | |
# to sites-enabled/ to enable it. | |
# | |
#server { | |
# listen 80; | |
# listen [::]:80; | |
# | |
# server_name example.com; | |
# | |
# root /var/www/example.com; | |
# index index.html; | |
# | |
# location / { | |
# try_files $uri $uri/ =404; | |
# } | |
#} |
在线22
,23
我们将更80
改为8080
。
nginx
listen 8080 default_server;
listen [::]:8080 default_server;
在启动 Nginx 服务之前,让我们快速测试一下配置。
bash
sudo nginx -t
如果一切顺利,我们来写一个小HTML
页面,让它告诉我们服务器已经启动了。Line41
显示了服务器的根目录。我修改了服务器根目录,以确保我使用的目录与 Apache 的目录不同。
root /srv/www/html;
这就是我将要添加文件的目录index.html
。现在我们可以启动服务器了。
sudo service nginx start
测试新的配置。
curl http://localhost:8080
它应该返回我创建的新HTML
文件。继续安装PHP
。
安装 PHP
目前我们已经在系统上安装了,由于整体和配置PHP7.2
有点令人担忧,将会转移到。PHP7.2
Nginx
PHP7.3
首先,Ubuntu 不知道从哪里获取,PHP7.3
所以我们需要添加存储库。
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade
实际安装PHP7.3
和一些扩展。这些只是我需要的扩展,您可以根据需要随意添加或删除扩展。
sudo apt install php7.3
sudo apt install php7.3-cli php7.3-fpm php7.3-pdo php7.3-mysql php7.3-zip php7.3-mbstring php7.3-curl php7.3-xml php7.3-bcmath php7.3-json
所有扩展安装完成后,就该Nginx
再次编辑配置文件,告诉它我们的网站使用了PHP
。配置文件如下所示,所有注释都被删除,只保留了更改行的注释。
server { | |
# Using port 8080 only for testing purposes | |
listen 8080 default_server; | |
listen [::]:8080 default_server; | |
# Where are the files that are being served | |
root /srv/www/html; | |
# Default Index file being served, have added `index.php` | |
index index.php index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# Added support for PHP routers | |
try_files $uri $uri/ /index.php?args =404; | |
} | |
# Adding support for PHP7.3 | |
location ~ \.php$ { | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
# Adding environmental variables for this website | |
include /srv/config/default.nginx.conf; | |
} | |
} |
server { | |
# Using port 8080 only for testing purposes | |
listen 8080 default_server; | |
listen [::]:8080 default_server; | |
# Where are the files that are being served | |
root /srv/www/html; | |
# Default Index file being served, have added `index.php` | |
index index.php index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# Added support for PHP routers | |
try_files $uri $uri/ /index.php?args =404; | |
} | |
# Adding support for PHP7.3 | |
location ~ \.php$ { | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
# Adding environmental variables for this website | |
include /srv/config/default.nginx.conf; | |
} | |
} |
server { | |
# Using port 8080 only for testing purposes | |
listen 8080 default_server; | |
listen [::]:8080 default_server; | |
# Where are the files that are being served | |
root /srv/www/html; | |
# Default Index file being served, have added `index.php` | |
index index.php index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
# Added support for PHP routers | |
try_files $uri $uri/ /index.php?args =404; | |
} | |
# Adding support for PHP7.3 | |
location ~ \.php$ { | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
# Adding environmental variables for this website | |
include /srv/config/default.nginx.conf; | |
} | |
} |
测试配置以确保没有语法错误。
sudo nginx -t
你可能会遇到错误,因为 处没有 env 文件/srv/config/default.nginx.conf
。现在只需在那里添加一个空白文件即可。稍后我会解释它的作用。
希望以上方法解决了您的错误。现在,我们将index.html
文件更改为index.php
,并添加一些基本PHP
代码,看看是否正常工作。另外,请务必为index.php
文件设置适当的权限。我喜欢这样做。
sudo chmod 755 index.php
重启Nginx
服务器
sudo service nginx restart
你应该可以看到PHP
你写的文件了。我把函数打印出来,phpinfo()
以确保配置正确。
服务器环境变量
我喜欢将一些应用配置保存在超全局变量中。当你在、和服务器$_SERVER
之间移动应用,并且不想频繁更改数据库密码或 API 端点时,这会非常方便。dev
stage
prod
我们接触的文件/srv/config/default.nginx.conf
,我将在其中添加以下代码行。
fastcgi_param APP_ENV dev;
fastcgi_param APP_ENDPOINT https://dev.server.com;
fastcgi_param DB_HOST localhost;
fastcgi_param DB_USER root;
fastcgi_param DB_PASS password;
格式通常是
fastcgi_param {VAR_NAME} {VAR_VALUE};
随意添加任何其他环境变量。测试配置完成后,重启Nginx
。你应该会在函数输出中看到新的变量,phpinfo()
如下所示。
密切关注最后 5 个值。
就这样,只需将所有文件移动到相关文件夹即可进行最终测试。准备就绪后,即可停止Apache
服务。
sudo service apache2 stop
8080
将配置中的端口号从 更改为Nginx
,80
然后重新启动Nginx
,就完成了。
做得好!
鏂囩珷鏉ユ簮锛�https://dev.to/davinderpalrehal/moving-from-apache2-to-nginx-webserver-2n6a