Nginx进阶篇·Nginx与Apache2共存

前言

今天接触了一个项目,它只能用Apache2,而且最近PHP8.2也推出了来了,所有想着试试就逝世的态度,结果还挺简单的,只能说配置好,后续再深入了解一下。

因此这里把简单的配置方案做一下。

环境

  1. Ubuntu

  2. Nginx

  3. PHP8.1-FPM

步骤

1.安装Apache2

add-apt-repository ppa:ondrej/apache2
apt install apache2

2.安装PHP8.2-fpm

apt install php8.2-fpm 
apt install libapache2-mod-php8.2

安装完提示有错误,正常,因为80端口被nginx占用了。

Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.
invoke-rc.d: initscript apache2, action "restart" failed.
× apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2022-10-12 10:04:59 CST; 22ms ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 121228 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)
        CPU: 12ms

Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: Starting The Apache HTTP Server...
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121231]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121231]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121231]: no listening sockets available, shutting down
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121231]: AH00015: Unable to open logs
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121228]: Action 'start' failed.
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121228]: The Apache error log may have more information.
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: apache2.service: Failed with result 'exit-code'.
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: Failed to start The Apache HTTP Server.
apache2_invoke: Enable module php8.2
Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.
invoke-rc.d: initscript apache2, action "restart" failed.
× apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2022-10-12 10:04:59 CST; 6ms ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 121262 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)
        CPU: 18ms

Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: Starting The Apache HTTP Server...
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121265]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121265]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121265]: no listening sockets available, shutting down
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121265]: AH00015: Unable to open logs
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121262]: Action 'start' failed.
Oct 12 10:04:59 VM-0-7-ubuntu apachectl[121262]: The Apache error log may have more information.
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: apache2.service: Failed with result 'exit-code'.
Oct 12 10:04:59 VM-0-7-ubuntu systemd[1]: Failed to start The Apache HTTP Server.

3.修改Apache2端口

vim /etc/apache2/ports.conf

修改为如下:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8081

<IfModule ssl_module>
        Listen 444
</IfModule>

<IfModule mod_gnutls.c>
        Listen 444
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

同时也要修改 VirtualHost 配置文件中的端口:

vim /etc/apache2/sites-enabled/000-default.conf

修改端口:

<VirtualHost *:8081>

这个时候可以看一下有没有因为端口问题报错。

systemctl restart apache2
systemctl status apache2

返回正常:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-10-12 10:13:47 CST; 7s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 124028 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 124032 (apache2)
      Tasks: 6 (limit: 8788)
     Memory: 10.3M
        CPU: 42ms
     CGroup: /system.slice/apache2.service
             ├─124032 /usr/sbin/apache2 -k start
             ├─124033 /usr/sbin/apache2 -k start
             ├─124034 /usr/sbin/apache2 -k start
             ├─124035 /usr/sbin/apache2 -k start
             ├─124036 /usr/sbin/apache2 -k start
             └─124037 /usr/sbin/apache2 -k start

Oct 12 10:13:47 VM-0-7-ubuntu systemd[1]: Starting The Apache HTTP Server...
Oct 12 10:13:47 VM-0-7-ubuntu systemd[1]: Started The Apache HTTP Server.

4.为apache2启用php-fpm

a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm

5.安装PHP模块

apt install php8.2-*

重启apache2

systemctl restart apache2

6.测试

到这边就可以试着用 IP:8081 进行访问了。

vim /var/www/html/index.php

输入:

<?php echo phpinfo();?>

保存退出。

编辑nginx的配置文件:

server {
    listen 80;
    index index.html index.htm index.nginx-debian.html index.php;
    server_name test.xxx.com;

    location / {
    proxy_pass_header Server;
        proxy_pass http://127.0.0.1:8081;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    access_log /var/log/nginx/test.xxx.com-access.log;
    error_log /var/log/nginx/test.xxx.com-error.log;    
}

重启nginx

systemctl restart nginx

输入网址就能访问了。

总结

Nginx与Apache2各有优缺点,具体还是要看需求。我查了下资料,Apache2和Nginx共存使其各司其职,没有想象中的那么完美,有一篇文章的结论是 Nginx+PHP-FPM > Apache2+Nginx+PHP-FPM。以后有空验证一下吧。

0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x
滚动至顶部