Setting up Web Hosting with Ubuntu and Apache

  Hosting  
 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. https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

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/id_rsa.pub  

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

~/.ssh
pwd
-> /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 example.com.conf

Setting up SSL

Enable SSL

sudo a2enmod ssl

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

root@yawebsite:/etc/apache2/sites-available# cat stage.yawebsite.com.au.conf

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

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

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.

Restart:

$ sudo service apache2 restart

Check the status:

$ sudo systemctl status apache2

References

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04
https://marketplace.digitalocean.com/apps/lamp
https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604
https://www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys


Web Development

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

Find out more