从 Apache2 迁移到 Nginx Web 服务器

2025-06-08

从 Apache2 迁移到 Nginx Web 服务器

最近,我不得不将公司开发、测试和生产环境的 Web 服务器从 Apache2 迁移到 Nginx。迁移的原因是,公司其他部门的后端服务都使用 Nginx,但在搭建网站时,由于某种原因,顾问们设置了 Apache2。这给管理层带来了很大困扰。在本文中,我将逐步讲解整个迁移过程。所有截图均使用 Vagrant Box 进行,但实际服务器的操作步骤与此相同。

服务器

我的测试服务器使用的是vagrantubuntu/bionic64。在这台服务器上,我之前有apache2.4php7.2mysql5.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



Enter fullscreen mode Exit fullscreen mode

实际安装 Nginx。



sudo apt install nginx


Enter fullscreen mode Exit fullscreen mode

Nginx 现在已经安装完毕,但尚未运行。由于我们已经在系统上安装并配置了 Apache2,Apache2 将使用端口 ,80这意味着为了避免冲突,我们应该在不同的端口上运行 Nginx,以测试一切是否运行正常。我选择了 端口8080

使用端口 8080 配置 Nginx

为了确保一切正常,我们默认使用 Nginx 设置了一个网站(与 Apache 类似)。您可以在此处查看配置。



sudo vim /etc/nginx/sites-available/default


Enter fullscreen mode Exit fullscreen mode

这应该会打开一个如下所示的文件。

##
# 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;
# }
#}
view raw default hosted with ❤ by GitHub
##
# 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;
# }
#}
view raw default hosted with ❤ by GitHub
##
# 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;
# }
#}
view raw default hosted with ❤ by GitHub

在线2223我们将更80改为8080


 nginx
listen 8080 default_server;
listen [::]:8080 default_server;


Enter fullscreen mode Exit fullscreen mode

在启动 Nginx 服务之前,让我们快速测试一下配置。


 bash
sudo nginx -t


Enter fullscreen mode Exit fullscreen mode

如果一切顺利,我们来写一个小HTML页面,让它告诉我们服务器已经启动了。Line41显示了服务器的根目录。我修改了服务器根目录,以确保我使用的目录与 Apache 的目录不同。



root /srv/www/html;


Enter fullscreen mode Exit fullscreen mode

这就是我将要添加文件的目录index.html。现在我们可以启动服务器了。



sudo service nginx start


Enter fullscreen mode Exit fullscreen mode

测试新的配置。



curl http://localhost:8080


Enter fullscreen mode Exit fullscreen mode

它应该返回我创建的新HTML文件。继续安装PHP

安装 PHP

目前我们已经在系统上安装了,由于整体和配置PHP7.2有点令人担忧,将会转移到PHP7.2NginxPHP7.3

首先,Ubuntu 不知道从哪里获取,PHP7.3所以我们需要添加存储库。



sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade


Enter fullscreen mode Exit fullscreen mode

实际安装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


Enter fullscreen mode Exit fullscreen mode

所有扩展安装完成后,就该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


Enter fullscreen mode Exit fullscreen mode

你可能会遇到错误,因为 处没有 env 文件/srv/config/default.nginx.conf。现在只需在那里添加一个空白文件即可。稍后我会解释它的作用。

希望以上方法解决了您的错误。现在,我们将index.html文件更改为index.php,并添加一些基本PHP代码,看看是否正常工作。另外,请务必为index.php文件设置适当的权限。我喜欢这样做。



sudo chmod 755 index.php


Enter fullscreen mode Exit fullscreen mode

重启Nginx服务器



sudo service nginx restart


Enter fullscreen mode Exit fullscreen mode

你应该可以看到PHP你写的文件了。我把函数打印出来,phpinfo()以确保配置正确。

服务器环境变量

我喜欢将一些应用配置保存在超全局变量中。当你在和服务器$_SERVER之间移动应用,并且不想频繁更改数据库密码或 API 端点时,这会非常方便。devstageprod

我们接触的文件/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;


Enter fullscreen mode Exit fullscreen mode

格式通常是



fastcgi_param  {VAR_NAME}      {VAR_VALUE};


Enter fullscreen mode Exit fullscreen mode

随意添加任何其他环境变量。测试配置完成后,重启Nginx。你应该会在函数输出中看到新的变量,phpinfo()如下所示。

PHP 服务器变量

密切关注最后 5 个值。

就这样,只需将所有文件移动到相关文件夹即可进行最终测试。准备就绪后,即可停止Apache服务。



sudo service apache2 stop


Enter fullscreen mode Exit fullscreen mode

8080将配置中的端口号从 更改为Nginx80然后重新启动Nginx,就完成了。

做得好!

鏂囩珷鏉ユ簮锛�https://dev.to/davinderpalrehal/moving-from-apache2-to-nginx-webserver-2n6a
PREV
Go:数组和切片,深入研究。
NEXT
一封写给软件比较和身份消亡的情书软件之所以美丽,是因为它本身热爱你自己每个角色都很重要我唯一认同的类比