Nginx 和 Apache 都是非常强大的 Web 服务器。Apache 稳定有强大的功能,而 Nginx 则具有轻量快速的特点。本文我们将介绍,如何结合两个 Web 服务器以获得最佳效果,我们将设置 Nginx 处理静态内容,而 设置Apache 来处理后端和动态内容。可结合牛奇网之前发布的另外一篇文章,如何在Ubuntu 18.04服务器上将Nginx配置为Web服务器和Apache的反向代理。
安装和配置 Nginx
首先更新存储库列表:
yum update -y
使用以下命令轻松安装 Epel 存储库:
yum install epel-release -y
使用以下命令安装 nginx:
yum install nginx -y
启用并设置为开机启动 Nginx 服务:
systemctl enable nginx
systemctl start nginx
配置 Nginx
使用以下命令创建配置文件:
nano /etc/nginx/conf.d/default.conf
将以下配置粘贴 Nginx 配置文件中,然后保存并退出:
server {
listen 80;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.pup$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
安装和配置 Apache
使用以下命令安装 Apache Web 服务器:
yum install httpd
启动并设置为开机启动 httpd 服务:
systemctl enable httpd
systemctl start httpd
配置 Apache
配置 Apache 以承担后端处理工作,为此我们需要配置 Apache 以侦听端口 8080:
nano /etc/httpd/conf/httpd.conf
找到以“listen”开头的行,将其删除并粘贴以下几行:
Listen 127.0.0.1:8080
然后找到以“DocumentRoot”开头的那一行,修改如下:
DocumentRoot "/usr/share/nginx/html/"
“DocumentRoot”在 Nginx 和 Apache 上应该相同,如果配置了 VirtualHost(s),则应该在它们上都配置。保存并退出。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/configure-nginx-and-apache-together-on-the-same-server/