How to configure VirtualHosts in Apache2 under Ubuntu Server.

Prerequisites

To start, you must ensure that you have a user that has sudo capabilities and that Apache2 is actually installed on your Ubuntu Server. To do this, you must run these commands at the linux terminal:

sudo apt update
sudo apt install apache2

Step 1 – Creating the folders

Once you have installed Apache, you can get to work creating the directory structure. On Ubuntu, web files are stored in the /var/www directory, so we will create all of our directories in here. Our top level html directories will have the same name as the URL of our subdomain, and we will create a public_html directory inside the top level directory, as this lets Apache know that the website needs to be available publicly.

For this example, we will use example.com and test.example.com; and we should made our directories like this:

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.example.com/public_html

Step 2 – Setting up user permissions

The directories that we have just created are currently owned by the user that created them (your sudo user) but they need to be changed so that people viewing your website can access them. To do this, run these commands:

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.example.com/public_html

By doing this with the $USER variable, regular users can now access the public_html subdirectory.

You should also change the permissions so that read access is permitted for general web viewers, to do this, run:

sudo chmod -R 755 /var/www

Step 3 – Create SMB shares to upload your websites

Now that you have your directories setup, you can create SMB shares to upload your HTML files to, so that you don’t have to copy them over the terminal. First, run:

sudo apt install samba samba-common-bin

Then, to check if it has installed correctly, run:

sudo smbstatus

If you get an output, samba has installed correctly. Such as:

Samba version 4.3.11-Ubuntu

You then need to setup your SMB shares, to do this you need to setup a share that requires a username and password to access, from either a windows PC, a linux box or a Mac. To do this, edit the file in nano.

sudo nano /etc/samba/smb.conf

Scroll right down to the bottom of the smb.conf file. Then, you need to give your share name as well as a few other parameters:

[www]

path = /var/www/
browseable = yes
guest ok = no
writable = yes
valid users = @samba
  • Path is the path of your web directory
  • Browseable is the parameter to tell clients that the folder exist (i.e. not hidden on the server.)
  • Guest ok = no means that you have to enter a username and password to access the share
  • Writable means that you can upload to the share
  • Valid users = @samba means that only users authorised to use Samba can use the share. We will do this now.

To authorise users on the Samba share, you need to run a command and use THE SAME USERNAME AS YOUR CURRENT USER. Mine for example, would be joe.

sudo smbpasswd -a joe

It will then prompt you to enter a Unix password, use the same password that you use to ssh into the server.

Then, create the group.

sudo groupadd samba

Now you need to add your Unix user to the user group samba, this authorises your account to use the shares. My username would be joe, so this is what it would look like for me.

sudo gpasswd -a joe samba

Now you need to setup the permissions so that people connecting to the share can upload files, to do this run this command:

sudo setfacl -R -m "g:samba:rwx" /var/www/

Then, check the parameters for syntax errors:

sudo testparm

Now all we have to do is restart the SMB server; run this command:

sudo systemctl restart smbd nmbd

You can now connect to the share. My server is called ‘lync’ and my username is ‘administrator’ so this is what me connecting to the share looks like from both my Mac and Windows computers:

Mounting the www share on a Mac.
Mounting the share on a Windows PC.

Step 4 – Creating your VirtualHost configuration files.

Unlike Windows WampServer, Apache on Ubuntu doesn’t use one single configuration file for vhosts, instead, each website has its own virtualhost configuration file. We will be configuring ours for our two domains: example.com and test.example.com.

You first need to create the files. I’ll start with example.com. Because this doesn’t have a subdomain, you’ll need to do example.com and www.example.com, like this:

sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
  ServerName example.com
  ServerAlias localhost
  DocumentRoot "/var/www/example.com/public_html"
  <Directory "/var/www/example.com/public_html">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias localhost
  DocumentRoot "/var/www/example.com/public_html"
  <Directory "/var/www/example.com/public_html">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

For test.example.com, it’s very similar, but you only need to do test.example.com, no www:

sudo nano /etc/apache2/sites-available/test.example.com.conf
<VirtualHost *:80>
  ServerName test.example.com
  ServerAlias localhost
  DocumentRoot "/var/www/test.example.com/public_html"
  <Directory "/var/www/test.example.com/public_html">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

Step 5 – Enabling your sites

Simply run these commands:

sudo a2ensite example.com.conf
sudo a2ensite test.example.com.conf
sudo systemctl restart apache2

You are now done! If you have any questions, don’t hesitate to contact me using the Contact Us page.

Comments

Leave a Reply

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