Adding a new website to your VPS via docker - apache2

Quick tutorial about how to enable a website via docker and apache2 using a reverse proxy configuration.

Adding a new website to your VPS via docker - apache2
Photo by Petter Lagson / Unsplash

Small tutorial about how to add a new website to your Apache2 web server with a ReverseProxy to a docker container.

Prerequisites

  • docker and docker compose.
  • apache2 and an existing apache2 site runing
  • certbot
  • domain and subdomain (subdomain.domain.com)
  • a VPS (virtual private server this tutorial assumes it is Ubuntu)

Create your site and containerize it

First off all create a new web site, in this case it will be a simple index.html file in a src folder:

 <!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>

<h1>Greetings</h1>
<p>This is a hello world 🌍 👋.</p>

</body>
</html> 

OK now lets create a docker-compose file to containerize 🏺 this awesome site:

version: "3"
services:
    client:
        image: nginx
        ports:
            - 8081:80
        volumes:
            - ./src:/usr/share/nginx/html

If everything goes well you could be able to do a docker-compose up -d and you will see your site in localhost:8081

Create a new Apache2 site with reverse proxy configuration

sounds complex but it is quite easy, first off all create a new site.conf inside /etc/apache2/sites-available, we will call it as my-site.conf and it will be full path as:

  • /etc/apache2/sites-available/my-site.conf
<VirtualHost *:80>
	ServerName <subdomain.domain>
	ServerAlias <yourAlias>
	ServerAdmin <ask@for.it>

	ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>
	ProxyPreserveHost on
	ProxyPass "/"  "http://127.0.0.1:8081/"
	ProxyPassReverse "/"  "http://127.0.0.1:8081/"
	RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
	RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

doesn't forget to change <> values for your relevant ones, and, the relevant ports in this case the calls to host in port 80 will be redirected to port 8081

Enable your site with sudo a2ensite my-site.conf (supposing you are inside folder /etc/apache2/sites-available ) and reload Apache with sudo systemctl reload apache2 command.

at this point your site is already in accessible but not through SSL.

Create a new SSL certificate for your new shiny site.

this is the last step you just need to generate a new SSL certificate with the command:

sudo certbot --apache -d subdomain.domain.com

Last reload Apache again with:

sudo systemctl reload apache2

and that's it your new site is ready to hit the word 🚀 🌎