apache2 reverse proxy

Jun 16, 2020 9:29 pm
by Alex

一直以來我都用 port number 來區分不同的 project, 最近換了 workflow, 用多了 virtual machine, 每一個 project 都有自己的 virtual machine, 自己的獨立 IP, 所以我試一下 reverse proxy, 原來簡單到不得了,只是以下幾個步驟便成功設定了 server.

首先要設定好 apache2, 如何設定 webserver 可以參考這個 blog post How to develop WordPress site locally

再 enable 以下的 mods

Some mods need to be enable

$sudo a2enmod proxy
$sudo a2enmod proxy_http
$sudo a2enmod proxy_ajp
$sudo a2enmod rewrite
$sudo a2enmod deflate
$sudo a2enmod headers
$sudo a2enmod proxy_balancer
$sudo a2enmod proxy_connect
$sudo a2enmod proxy_html

make a copy of the 000-default.conf file

$sudo cp /etc/apache2/sites-available/000-default.conf default.conf
sudo nano /etc/apache2/site-aviliable/default.conf

Add the following virtual host directive to the config file

# Virtual Host for alextech.tips
<VirtualHost *:80>
        ServerName alextech.tips
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://10.0.38.117/
        ProxyPassReverse / http://10.0.38.117/
</VirtualHost>

# Virtual Host for studio.comp-sq.com
<VirtualHost *:80>
        ServerName studio.comp-sq.com
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://10.0.38.102/
        ProxyPassReverse / http://10.0.38.102/
</VirtualHost>

# Virtual Host for wiki.comp-sq.com
<VirtualHost *:80>
        ServerName wiki.comp-sq.com
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://10.0.38.102:8080/
        ProxyPassReverse / http://10.0.38.102:8080/
</VirtualHost>

<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 www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	# 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

Disable the 000-default.config
Enable the default config file

$sudo a2dissite 000-default.conf
$sudo a2ensite default.conf
$sudo systemctl restart apache2

EXTRA NOTES

Enable root access for SSH

sudo nano /etc/ssh/sshd_config

find PermitRootLogin set it to yes, add the following line to file

PermitRootLogin yes

$sudo systemctl restart ssh

remember to turn off root access when finish config the server, add a # in-front to disable root access

#PermitRootLogin yes

$sudo systemctl restart ssh