VPS Set Up Guide Part 2: Setting up Your Site

Step by step guide to setting up your sites on Ubuntu 12.04

This is part 2 of a 2 part guide. If you've not been through part 1 first, you might want to check it out!

In this guide, we'll be focussing on setting up an actual site on our server, along with some basic firewall rules.

Uncomplicated Firewall (UFW) setup

UFW is a much easier way to set up and manage your server's firewall than IPtables. If you don't have UFW installed already, use apt-get to get it:

sudo apt-get install ufw

By default, UFW will be disabled. So, first things first, enable it:

sudo ufw enable

Don't worry, it'll still allow your current connections. You won't be instantly locked out!

Setting up a firewall is very need's specific. I'll just show the basic rules I use as a base, but you way want to look at the other rules available if you need something different.

// Sets the default incoming traffic to deny
sudo ufw default deny

// Allows normal web access
sudo ufw allow 80

// Allows SSH
sudo ufw allow ssh

// You might want to lock ssh down to a specific IP
// If so, use this instead
sudo ufw allow from to any port 22

obviously changing to your IP.

The rules above allow all outgoing connections, but only incoming connections to port 80 and 22.

Setting up MySQL databases

If your site needs a MySQL database, you'll need to set one up.

First, log into MySQL using the default username/password you set up when installing it.

mysql -u root -p
// You'll be prompted enter your password now

Next, we'll create a new user, along with a new database, and grant that user permissions for that database. It's a good idea in general to create a new user per site, and only give that user permissions to select databases. This will prevent anyone who gains access to one of your databases from having access to any of the others.

// Create a new user
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

// Create a new database
CREATE DATABASE database_name;

// Grant the user privileges
GRANT ALL on 'database_name'.* TO 'username'@'localhost';

The new user has all privileges on all tables in that database. To create a more restricted user, look at the MySQL GRANT docs.

Exit out of mysql, and try to log back in as the new user:

// Log out
mysql exit;

// Log in as your new user
mysql -u username -p
// Enter your password

// Show the user's databases
show databases;

It should show your new database, and only your new databases.

Now that you've a user set up, and a database, you need to import your tables for your site.

I cheat a bit at this point, and I upload a copy of adminer, which I find to be much easier to use than faffing about on the command line.

If you don't want to use a GUI, simply log in as the database user, select the database, and paste in the sql to create the tables.

Upload your site

Time to upload the site to the server.

First, create a new directory:

sudo mkdir /var/www/sitename.com

Connect to your server via sftp, using the details you connect to the server with when ssh'ing, and upload your files into the new directory.

Setting up the vhost

Multiple sites can be hosted on your VPS, all they need is their own vhost.

If you haven't pointed your DNS to your server, you should do that now. Here's how if you're not sure.

First, go to the sites-available directory:

cd /etc/apache2/sites-available

The sites-available directory is where all the vhosts are stored. However, just because a vhost is in the sites-availabe directory, doesn't mean it's active. This will be explained in more detail later on.

Before we create our site, it's worth explaining a little how vhosts work. Inside the sites-available folder, there's already a default vhost. If you go to your domain in a browser (and you've pointed your DNS to your VPS), what you see is defined by the default vhost.

If no vhost is set for a specific domain, it'll load the first vhost that's enabled, which in this case is the default, as it's the only one.

Setting up a site

There are two stages to setting up a site: creating the vhost, and enabling it.

// Copy the default vhost
sudo cp default sitename.com

// Edit it
sudo nano sitename.com

Inside the vhost, it should be fairly self explanatory what to edit, and you should end up with something like this:

<VirtualHost *:80>

DocumentRoot /var/www/sitename.com/
<Directory /var/www/sitename.com/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

ErrorLog ${APACHE_LOG_DIR}/sitename-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/sitename-access.log combined


Save the file, and exit nano.

Now that the vhost is set up, you need to enable it:

sudo a2ensite sitename.com

That's it! If you head to the site in a browser, you should see it all working.

If you need a MySQL database, read below on how to set one up.

Bonus - Modifying the default vhost

Personally, I edit the default vhost to guarantee it always loads first, and load a simple landing page with a link to my main site on it.

// Disable the site first
sudo a2disite default

// Create a new directory to store your landing page
sudo mkdir /var/www/00_default

// Rename the vhost to 00_default, so it'll always load first
sudo mv default 00_default

// Edit the vhost to point to your new directory
sudo nano 00_default

// Enable the site
sudo a2ensite 00_default

Now, add whatever you want to display in /var/www/00_default.

Round up

That's pretty much it in terms of setting up a VPS.

Of course, there's lots more to play around with, but we've covered everything to get started and run a simple site.

If there's anything else you want to see covered, or anything you think can be done better, let me know in the comments!

Want to let me know what you think of VPS Set Up Guide Part 2: Setting up Your Site? Why not leave a comment, follow me on Twitter , or !


comments powered by Disqus