使用hyperf的时候发现它监听9501端口,然后这样需要ip+port方式去访问,但是这样对用户而言有点不太友好,如果我们还有域名,可以做一个反向代理避免端口直接写出来。
找了找网上别人写的例子,感觉都太不细致了,还是自己写一个吧。
例子如下:
1.查找 sudo apt search apache2
Sorting... Done Full Text Search... Done apache2/focal-updates,focal-security,now 2.4.41-4ubuntu3.1 amd64 [installed] Apache HTTP Server
2.安装 sudo apt-get install apache2
3.查看 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 Fri 2021-06-18 13:26:06 CST; 2min 20s ago Docs: https://httpd.apache.org/docs/2.4/ Process: 41948 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 41952 (apache2) Tasks: 6 (limit: 19023) Memory: 17.8M CGroup: /system.slice/apache2.service ├─41952 /usr/sbin/apache2 -k start ├─41959 /usr/sbin/apache2 -k start ├─41960 /usr/sbin/apache2 -k start ├─41961 /usr/sbin/apache2 -k start ├─41962 /usr/sbin/apache2 -k start └─41963 /usr/sbin/apache2 -k start
4.启动 apache2 如有必要关闭 nginx
systemctl stop nginx.service
5.开启各种重写反向代理模块
sudo a2enmod rewrite
提示我已经开了(因为以前开过点这里)
Module rewrite already enabled
sudo a2enmod proxy
Enabling module proxy.
To activate the new configuration, you need to run:
systemctl restart apache2
sudo a2enmod proxy_http
Considering dependency proxy for proxy_http: Module proxy already enabled Enabling module proxy_http. To activate the new configuration, you need to run: systemctl restart apache2
按它说的重启一下apache2
systemctl restart apache2
重启完成查看已经安装的Module
apache2ctl -M |grep pro
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message proxy_module (shared) proxy_http_module (shared)
apache2ctl -M |grep rew
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message rewrite_module (shared)
6.重启apache2
sudo /etc/init.d/apache2 restart
Restarting apache2 (via systemctl): apache2.service.
7.做反向代理的一个vhost配置
编辑 /etc/hosts
127.0.0.1 local.hyperf.com
创建并编辑 /etc/apache2/sites-enabled/local.hyperf.com.conf
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName local.hyperf.com ServerAdmin local.hyperf.com DocumentRoot /var/www/html/hyperf ProxyPreserveHost On ProxyPass / http://0.0.0.0:9501/ ProxyPassReverse / http://0.0.0.0:9501/ # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
8.重启apache2
sudo /etc/init.d/apache2 restart
Restarting apache2 (via systemctl): apache2.service.
9.访问 http://local.hyperf.com/ 发现成功