Host multiple websites on Ubuntu – Apache version

New guide on how to how host multiple websites on Ubuntu server, with Apache. In this guide we’ll cover how to install two websites but you can do more, the concept is the same.

Installing Apache

The first thing you want to do is updating the system.

sudo apt update

Now install the Apache server

sudo apt install apache2

Opening the ports

Now that you have installed the server you have to open the ports so:

sudo ufw allow 80

Move the websites or create new

First thing is to create a directory where you’ll put the websites:

sudo mkdir /websites

Now in here we recommend creating the websites directory name with the domain in it for example:

cd /websites
sudo mkdir /mydomain.com

Now using FTP or SFTP move the websites from your pc to the vps or simply create your websites from the vps. (we won’t cover how to do basic html code)

Giving permissions

Now you have to make sure that the server has access to the files, so run:

sudo chown -R www-data:www-data /websites

Virtual host File

Last thing is to create the virtual host file, which is one important thing to host multiple websites on ubuntu. Navigate to:

cd /etc/apache2/sites-available/

Here you can make one single file for all the websites or make n files for n websites, however we recommend creating separate files.

sudo nano mydomain.com.conf
<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /websites/mydomain.com/public_html

    <Directory />
        Options FollowSymLinks
        AllowOverride All-
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mydomain.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mydomain.com-access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME}=www.mydomain.com [OR] RewriteCond %{SERVER_NAME}=mydomain.com

</VirtualHost>

Name the file as you like we still recommend naming them with the domain name.

Now you have to enable those configuration files:

sudo a2ensite mydomain.com.conf

Now remove the basic configuration:

sudo a2dissite 000-default.conf

Enable the URL Rewrite

sudo a2enmod rewrite

 

Restart apache

sudo systemctl restart apache2

Conclusion

So, this was how host multiple websites on the same machine with Apache running on Ubuntu server. In the next guide we’ll cover how to setup SSL, which means stay tuned and see y’all in the next one.