前言

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

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

环境

  1. Ubuntu
  2. Nginx
  3. PHP8.1-FPM

步骤

1.安装Apache2

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

2.安装PHP8.2-fpm

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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端口

1
vim /etc/apache2/ports.conf

修改为如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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 配置文件中的端口:

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

修改端口:

1
<VirtualHost *:8081>

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

1
2
systemctl restart apache2
systemctl status apache2

返回正常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm

5.安装PHP模块

1
apt install php8.2-*

重启apache2

1
systemctl restart apache2

6.测试

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

1
vim /var/www/html/index.php

输入:

1
<?php echo phpinfo();?>

保存退出。

编辑nginx的配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
systemctl restart nginx

输入网址就能访问了。

总结

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。