Ubuntu Virtual Server Walkthrough
REVISED 2018–02–22: Use apt-get to install required Perl modules rather than building them via CPAN, which means we no longer need to install build-essential
. Also, document differences if you only want to host a single site instead of multiple virtual sites.
Introduction
Everyone has their own approach to managing a website. It probably goes without saying that I want to use MultiMarkdown to manage mine. After many other workflows, I created an ultra lightweight CMS which is basically a wrapper around MMD text files (MultiMarkdown-CMS).
I use git for revision tracking, and for allowing me to work on my desktop or laptop without messing up the production site until I’m ready. But in order to see how my changes look, I need a staging server to try it out. I have previously used my main Mac desktop for this, but with every major macOS update, things tend to break.
I’ve heard rumors that Apple may be moving away from making it easy to enable “server” features on your Mac (e.g. Apache). This may or may not be true, but it did help prompt to consider alternatives
More recently, I have configured an Ubuntu virtual server using VirtualBox on my Mac. This should make it easier to maintain, and will not require any special effort whenever I update (or change) my Mac desktop. I can simply restart the virtual machine after upgrading macOS, and all is good. If I get a new machine, simply copy the files over, install VBox again, and all is good.
Caveats
I will take some knowledge for granted. You should have some experience with the command line, Apache, and Git. You should be able to find more detailed information for all steps via Google if you’re having any trouble.
The purpose of this guide is to provide a brief walkthrough combining different steps for the purpose of creating a web server capable of handling my workflow. It’s not a complete guide to the many component parts involved. Your needs may vary. I don’t claim this is the best way, simply one of many ways to configure things.
It’s also a chance for me to take some notes for myself for future reference. My needs are for a staging server on my private network, behind a firewall. If you are doing anything facing the internet, you’ll want to be sure that it is secure. That is beyond the scope of this document.
The Virtual Server
You can use whichever virtualization app suits your needs. I’ll use VirtualBox because it’s free and works well enough for my needs.
I use Ubuntu because it’s what I have the most experience with, and seems to have a great deal of online support scattered out there. Use whatever you like.
Ubuntu – I don’t want a GUI on this machine, so I used the server version. I want it to last, so I used the LTS version (16.04 at the time I set it up).
RAM – I used 1 Gig. Probably overkill to be honest
Disk space – 15 Gigs. Also overkill, but I wanted to be able to use this same base machine for other purposes
Installation
Follow the installation sequence in Ubuntu. Use whatever is best for your needs, but I used:
Guided disk partitioning with LVM (Did not use encryption because this is already “inside” an encrypted drive and it wasn’t important to me.)
I DID NOT enable automatic updates – I want to manage this manually to avoid breaking things at inopportune times.
I selected only the OpenSSH package to start with (I did not use LAMP because I don’t need MySQL or PHP), and left the “standard system utilities” selected
Then reboot into your server.
Basic Configuration
Log in as your new user and then:
sudo apt-get update
– update the apt-get databasesudo apt-get upgrade
– upgrade any installed packages to newest available versionssudo apt-get install apache2 libcgi-session-perl libio-string-perl
– install Apache as well as two required Perl modules
Apache Configuration
sudo a2enmod include rewrite cgi headers
– enable some basic Apache modules
Firewall Configuration
(Again, use what is right for you)
sudo uwf allow OpenSSH
– enable SSH access through firewallsudo uwf allow "Apache Full"
– enable Apache access through firewallsudo uwf enable
Configure Apache for Virtual Hosts
I wanted to be able to host multiple sites on this server, rather than creating separate virtual machines for each hosted site. If you would rather host only a single
site, you can store your files in /var/www/html
instead of creating these directories. You will need to make a change to etc/apache2/apache2.conf
:
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
should become:
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
If you’re only going to host a single site, you can then skip the rest of this section.
(I used https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts to configure virtual hosts in Apache 2)
cd /etc/apache2/sites-available
sudo cp 000-default.conf <site>.conf
– (where<site>
is the hostname for the site, e.g.staging.private
orstaging.local
)sudo pico <site>.conf
– (use whatever editor you prefer)Set
ServerAdmin
as appropriate.Set
ServerName
to your desired hostnameAdd
ServerAlias <site>
for any additional hostnames you are usingSet
DocumentRoot
to/var/www/<site>/public_html
After the <VirtualHost>
declaration, add:
<Directory /var/www/<site>/public_html>
AllowOverride All
</Directory>
Save your changes
sudo a2ensite <site>.conf
Load Sample Content to Web Site
cd /var/www
If you are hosting multiple sites:
sudo mkdir -p <site>/public_html
sudo chown -R <user>:<user> <site>/public_html
sudo chmod -R 755 /var/www
pico /var/www/<site>/public_html/index.html
(orpico /var/www/html/index.html
if using the single site approach) and save the following:<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com virtual host is working!</h1> </body> </html>
At this point, shutdown your virtual machine:
sudo shutdown -r now
Configure Networking
Your virtual machine needs to be able to communicate with your local network. In VirtualBox, I change the first Network Adapter to Bridged Adapter
instead of NAT
.
Take note of the MAC Address
– this allows you to configure your DHCP server on your network (perhaps in your wireless base station?) to assign a consistent IP address to this particular virtual machine (using your wireless base station, etc.)
If you have your own DNS server, add your <site>
as a hostname for the IP address you assigned to the virtual machine. Otherwise, you can use the hosts file (on Mac or *nix machines) to accomplish the same thing. (Outside the scope of this document).
Next Steps
Go ahead and reboot your virtual machine and log in. Upgrade the installed packages with apt-get
if new ones are available, as above.
If your network configuration is working correctly, you should be able to log in with both of these:
ssh <ip address>
ssh <host name>
(Troubleshooting this if it doesn’t work is outside the scope of this document.)
You should now also be able to locate the main server Apache page in a web browser at http://<ip address>/
. (You can use ifconfig
from the server to find it’s IP address.)
If you set up the DNS/hosts correctly, you should also be able to access http://<site name>/
and see the placeholder page you created.
Install MultiMarkdown-CMS Site
Now that you can access the placeholder site, it’s time to install the actual web site you have previously built.
cd /var/www/<site>/public_html
(orcd /var/www/html
for single site)rm index.html
– this directory needs to be emptygit clone <path/to/your/site> .
– this will clone your existing git site to the desired location (assuming you already have it configured as a git repo, otherwise move the files here however you prefer.)
Now, when you use a browser to visit http://<your hostname>/
, you should see the live MMD-CMS site! (http://<ip address>/
will still show the main Apache page)
Alternative Approach
If you have not used MultiMarkdown-CMS before, you can create a new site this way:
git clone https://github.com/fletcher/MultiMarkdown-CMS.git .
I would recommend using the advanced branch( git checkout advanced
).
You can now start customizing your site as desired!
Advanced git Tricks
I tend to work on my website on my desktop machine, and then push my changes to the production server via git. This requires a bit of trickery to ensure that the rest of the world sees your latest changes. The post-update
hook below should cause git to refresh the contents of the web directory any time you push an update to the master branch in the production server.
cd /var/www/<site>/public_html/.git/hooks
pico post-update
- Save the following to the file:
#!/bin/sh cd /var/www/<site>/public_html /usr/bin/env -i /usr/bin/git reset --hard chmod g+s cgi/accept_comment.cgi
- Save the following to the file:
chmod 755 post-update
Add the following to
.git/config
in order to allow you to push to it:[receive] denyCurrentBranch = ignore