前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx进阶篇·Nginx与Apache2共存

Nginx进阶篇·Nginx与Apache2共存

原创
作者头像
我不是程序员110
发布2022-10-28 10:15:14
1.4K0
发布2022-10-28 10:15:14
举报
文章被收录于专栏:Ubuntu建站指南Ubuntu建站指南

前言

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

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

环境

1.Ubuntu

2.Nginx

3.PHP8.1-FPM

步骤

1.安装Apache2

代码语言:shell
复制
add-apt-repository ppa:ondrej/apache2
apt install apache2

2.安装PHP8.2-fpm

代码语言:shell
复制
apt install php8.2-fpm 
apt install libapache2-mod-php8.2

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

代码语言:text
复制
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端口

代码语言:text
复制
vim /etc/apache2/ports.conf

修改为如下:

代码语言:text
复制
# 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配置文件中的端口:

代码语言:text
复制
vim /etc/apache2/sites-enabled/000-default.conf

修改端口:

代码语言:text
复制
<VirtualHost *:8081>

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

代码语言:shell
复制
systemctl restart apache2
systemctl status apache2

返回正常:

代码语言:text
复制
● 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

代码语言:shell
复制
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm

5.安装PHP模块

代码语言:shell
复制
apt install php8.2-*

重启apache2

代码语言:shell
复制
systemctl restart apache2

6.测试

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

代码语言:text
复制
vim /var/www/html/index.php

输入:

代码语言:php
复制
<?php echo phpinfo();?>

保存退出。

编辑nginx的配置文件:

代码语言:text
复制
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

代码语言:shell
复制
systemctl restart nginx

输入网址就能访问了。

总结

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 环境
  • 步骤
    • 1.安装Apache2
      • 2.安装PHP8.2-fpm
        • 3.修改Apache2端口
          • 4.为apache2启用php-fpm
            • 5.安装PHP模块
              • 6.测试
              • 总结
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档