On occasion we deploy websites to a VPS running Ubuntu. In these cases we have to manually set up the web server via the command line - here's our annotated list of setup commands.

For websites that need extra power to handle heavier traffic loads or those that require more specific technical setups we look to utilising Virtual Private Servers (VPS) that web hosting companies now commonly provide.

Running a website on a VPS means that we can set up the hosting platform however we need, but it also means that we need to do the work ourselves to initially install and configure the webserver and database. When a new VPS is ordered it's effectively a fresh install of a server focused operating system, such as CentOS, Fedora, Ubuntu or Windows Servers. Some hosts will also offer the installation of cPanel, Plesk or equivalent, and even manage the whole setup for you.

Our preference is to use an LTS version of Ubuntu, without cPanel.

Each time we've set up a server we've recorded the commands and steps we've taken, refining the notes each time.

Below is a heavily annotated series of steps we take to set up an Apache based webserver on Ubuntu. This is NOT a complete shell script as such, it's simply a list of the commands you would use. It's also really just the first half of our notes, that covers the initial setup of the server, not the deployment of a site.

This list of steps is assuming that you've been allocated a new VPS with Ubuntu installed and a root SSH login. Amazon's EC2, for example, provides a user with sudo privileges automatically, but the notes below include steps to create a new user if your install doesn't create one for you. This is also intended for a system where only one website is being run on the VPS.

Setting up Ubuntu for use as a web server:

#update distro, either as root or sudo these commands
apt-get update
apt-get upgrade

#reboot the server at this point and log back in

# create a new user for doing sudo commands, replace USERNAME with your desired username
adduser --shell "/bin/bash" USERNAME
echo "USERNAME ALL=(ALL:ALL) ALL" >> /etc/sudoers
echo "AllowUsers USERNAME" >> /etc/ssh/sshd_config
service ssh restart

# relogin as new user

# edit sshd config, change PermitRootLogin yes to no
# also change to non-default port (between 1024 and 65536)
sudo nano /etc/ssh/sshd_config
sudo service ssh restart

# relogin to the server using the new IP address, e.g. 
# ssh username@hostip -p 12345

# install lamp-server using tasksel
sudo tasksel install lamp-server

# select root password for mysql

# install commonly used/expected libraries for PHP, plus unzip, include other packages here as you see fit
sudo apt-get install php5-suhosin php5-gd php-apc libcurl3 php5-curl unzip

# if you are planning to use Amazon's S3 for backups, install:
sudo apt-get install s3cmd

# enable mod-rewrite for apache
sudo a2enmod rewrite

# set timezone on server
sudo dpkg-reconfigure tzdata

# edit php default and change timezone to match
sudo nano /etc/php5/apache2/php.ini

# edit the default hosting settings
sudo nano /etc/apache2/sites-available/default

# make look as per the following:
<Directory /var/www/>
       Options FollowSymLinks MultiViews
       AllowOverride all
       Order allow,deny
       allow from all
</Directory>

# restart apache
sudo service apache2 restart

# at this point apache should be running and serving pages/php, able to use mod_rewrite for things like pretty urls, as per .htaccess files

The above script gets you a working webserver, with MySQL available for importing your database. In a future post I'll document how to transfer a website (most likely a concrete5 site) to this web hosting, set up an SSL certificate and enable https.

What I'd also love to do is further improve the notes (or fix any actual errors) - suggestions, comments (and polite criticisms!) welcome.

-Ryan