Setting up Web Hosting with Ubuntu and Apache

 Jun 2019

Setting up hosting on a linux VPS with Ubuntu and apache is not too hard, as long as you stay on the path. Grab a box at DO and choose the Lamp App which will set you up with apache, php, mysql etc.

Set up Users

Follow the guide to create a new Sudo user. You don’t want to use root for everything. Give them a nice fat password and follow the prompts.

adduser barry
usermod -aG sudo barry

Become barry, and test his powers(regular jo’s can’t run ls on root)

su - barry
sudo ls -la /root

Set up SSH Keys.

I like to just copy them over manually so I can see exactly what has happened. So, on your local:

cat ~/.ssh/  

SSH into local (using root that DO setup, password or whatever), and su the user, and do this:

-> /home/barry/.ssh
echo public_key_string_copied_from_local_ctl-c >> ~/.ssh/authorized_keys
chmod -R go= ~/.ssh
chown -R barry:barry ~/.ssh
sudo systemctl restart ssh

Set up apache to serve Live & Stage

Apache will server a site for each .conf file in sites-available. Copy the default and add your own ServerName and ServerAlias (the www subdomain for live sites, none for stage).

Once done, enable each site:

sudo a2ensite

Setting up SSL

Enable SSL

sudo a2enmod ssl

Add the port 443 section to your .conf for stage and live.

[email protected]:/etc/apache2/sites-available# cat

<VirtualHost *:80>
    <Directory /var/www/stage/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    ServerAdmin [email protected]
    DocumentRoot /var/www/stage/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<VirtualHost *:443>
    <Directory /var/www/stage/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    DocumentRoot /var/www/stage/public_html
    SSLEngine on
    SSLCertificateFile /var/www/stage/ssl/certificate.pem
    SSLCertificateKeyFile /var/www/stage/ssl/key.key

Upload your certificate.pem and key.key files (eg from cloudflare’s edge certificate feature) to the folders shown in the aboe configs - for both stage and live.


$ sudo service apache2 restart

Check the status:

$ sudo systemctl status apache2


Web Development

Got a web project you need doing well? We can help with that!

Find out more