Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active May 8, 2022 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/24ac833d0df0ff9b5737977b91229506 to your computer and use it in GitHub Desktop.
Save gilangvperdana/24ac833d0df0ff9b5737977b91229506 to your computer and use it in GitHub Desktop.
Apache2 / HTTPD Server Block

All About Apache / Httpd

Baremetal

  • Installation
apt install -y apache2
  • Configuration on /etc/apache2/

with Docker

  • Installation
apt install docker
docker run --name apache -d -p 80:80 httpd
  • Configuration
docker exec -it apache bash
cd /usr/local/apache2

Apache VHost

  • Baremetal
cd /etc/apache2/sites-enabled/000-default.conf on Baremetal
  • Docker
cd /usr/local/apache2/conf/httpd.conf

Uncomment Include conf/extra/httpd-vhosts.conf then,

cd /usr/local/apache2/conf/extra/httpd-vhost.conf

Example Block with 2 Domain & Restrict

  • Goal
    • Use one IP for multiple domain.
    • Another domain except declare domain can't access.
    • Can access res/index.html with res.gbesar.com
    • Can access res2/index.html with res.bignetlab.my.id
<VirtualHost *:80>
    ServerName catchall
    <Location />
        Require all denied
    </Location>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/htdocs/res"
    ServerName res.gbesar.com
    ServerAlias www.res.gbesar.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/htdocs/res2"
    ServerName res.bignetlab.my.id
    ServerAlias www.res.bignetlab.my.id
</VirtualHost>

Another

  • Reverse Proxy
    • Activate Module

      sudo a2enmod proxy
      sudo a2enmod proxy_http
      sudo a2enmod proxy_balancer
      sudo a2enmod proxy lbmethod_byrequest
      sudo service apache2 restart
      
    • Activate/Uncomment these 2 module if you run httpd on Docker

      nano /usr/local/apache2/conf/httpd.conf
      
      LoadModule proxy_module modules/mod_proxy.so
      LoadModule proxy_http_module modules/mod_proxy_http.so
      
    • Define on VHost Configuration

      ProxyPass / http://<ip_server>:<port>/
      ProxyPassReverse / http://<ip_server>:<port>/
      
    • Final Block Configuration

      • Goals:
        • Access /hello to access 172.17.0.4 content
        • Access / to access content on /usr/local/apache2/htdocs/res2
      <VirtualHost *:80>
        DocumentRoot "/usr/local/apache2/htdocs/res2"
        ServerName res.bignetlab.my.id
        ServerAlias www.res.bignetlab.my.id
        ProxyPass /hello http://172.17.0.4
        ProxyPassReverse /hello http://172.17.0.4
      </VirtualHost>
      

Implement TLS/SSL

Click Here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment