ubuntu 下做反向代理給hyperf使用

使用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/ 發現成功

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章