Adding a new website under Apache

Apache

Apache is a great piece of website software. What I like the best, is that it is mature and reliable. It is also easy to find documentation. Its configuration has been stable for a long time. Today, when you are supposed to upgrade on a daily basis, and are considered “stale” if you don’t pivot to new functionality every six months, its almost unique to be as “unchanging”, and thereby rock-solid, as Apache.

So even though NGinx is hotter, Apache is always my go-to solution when I set up my own sites. At least when on my own hardware.

This article is about a common situation where you already have Apache up and running, and supposedly PHP and mySQL added, and you want to setup a new site. In this case I have a domain “demo.fsh.se” and I want it to point to a new WordPress site.

This instruction is general, and not exclusively for WordPress, so the same procedure is used for a general static HTML or a compiled ReactJS site.

So I unzip my HTML files (WordPress or whatever) to /var/www/html/demo.fsh.se. Set owner of all files to www-data:www-data (or whatever user you have running apache. It must be able to access the files of course).

Apache Virtual Host

The next step is to add a “virtual host” (apache terminology). A virtual host enables Apache to detect incoming calls on port 80, reroute them to the correct site. This way, you can have Apache being the operator, and disguise the fact that you have multiple sites behind the same IP.

Apache identifies the virtual website using the domain name sent by the browser in its HTTP GET request. So this does not work when you access the site using its IP address!

Begin by copying the template

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo.fsh.se.conf

Then edit this config file

sudo nano /etc/apache2/sites-available/demo.fsh.se.conf

Add a section similar to this (change details that applies to your installation)

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 ServerName www.demo.fsh.se
 ServerAlias www.demo.fsh.se
 DocumentRoot /var/www/html/demo.fsh.se
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Use an Apache script to enable the new virtual host

sudo a2ensite example.com.conf

Then restart the apache server

sudo service apache2 restart

Alternatively, you can use

sudo systemctl restart apache2

Read here about the differences: https://stackoverflow.com/questions/43537851/difference-between-systemctl-and-service-command

Last, add demo.fsh.se to your /etc/hosts file (or use a dynamic DNS service like www.dyndns.com) unless you have a proper DNS entry for it already

Adding a firewall

I highly recommend using a firewall, and ufw (universal firewall) is so easy to use that you cannot go wrong.

Check whether it is installed using:

sudo ufw status

If needed, install it using:

sudo aot install ufw

However if it is installed and it says “Status: inactive” you can enable it using:

sudo ufw enable

If you need to open a port, you type:

sudo ufw allow <portnumber/keyword>

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80                         ALLOW       Anywhere                  
8888                       ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
80 (v6)                    ALLOW       Anywhere (v6)             
8888 (v6)                  ALLOW       Anywhere (v6) 




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.