Avatar

VPS Tutorial

So far, this website has been hosted on a dynamic hostinc account. Now I've ordered a VPS (Virtual Private Server). Most common options are Static hosting; Dynamic hosting; VPS; Dedicated server or a Colocated server.

Dynamic hosting is pretty much the same as static hosting, but containing PHP and database support, mostly MySQL. With a colocated server, you have your own physical server, but when hardware is defective, you'll have to drive to the datacenter yourself. A dedicated server is pretty much the same thing, but with this, the hoster will take care of delivery and maintenance. A VPS is a dedicated server hosting various virtual machines, with a maximum of 5 in my case. You'll need a command-line interface (Putty) to install an operating system (Linux) and the rest of the server applications. The applications I need, you'll find below:

Table of contents

VPS Reinstall

If there's an operating system on the VPS, this one has to be shutdown first. If you choose reimage, you'll get a list with available distributions. I chose Ubuntu 9.04 (Jaunty Jackalope). This tutorial will also be useful if you choose Debian. Maybe some files will be located elsewhere. This tutorial assumed you are logged in as root. If you aren't, some of the commands like `apt-get` can't be executed without putting `sudo` in front of it.

1shutdown
2reimage
3# Choose
4I wish to reinstall vpsxxx
5# Remember password
6boot

Console

To login on the virtual machine, I'll change the password immediately, because the given password won't be easy to remember:

1console
2# Login with the acquired password
3passwd
4# Enter the new password twice

We can use the console to add new applications.

Midnight Commander

With Midnight Commander you have an interface like Windows Explorer to browse through your files.

1apt-get install mc

Nano

Nano is a text editor that will come in handy when editing configuration files. It may be already installed. In Midnight Commander you can use Alt+4 to edit files.

1apt-get install nano

Apache

The web server.

1apt-get install apache2

By default, Mod Rewrite isn't installed. With the default settings, `.htaccess` isn't supported:

1a2enmod rewrite

To support `.htaccess`,`/etc/apache2/sites-available/default` has to be edited:

1<virtualhost *:80>
2ServerAdmin webmaster@localhost
3DocumentRoot /var/www
4
5<directory>
6    Options FollowSymLinks
7    AllowOverride All
8</directory>
9
10<directory "/var/www">
11    Options Indexes FollowSymLinks MultiViews
12    AllowOverride All
13    Order allow,deny
14    allow from all
15</directory>
16
17ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
18
19<directory "/usr/lib/cgi-bin">
20    AllowOverride None
21    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
22    Order allow,deny
23    Allow from all
24</directory>
25
26ErrorLog /var/log/apache2/error.log
27# Possible values include: debug, info, notice, warn, error, crit,
28# alert, emerg.
29LogLevel warn
30CustomLog /var/log/apache2/access.log combined
31
32Alias /doc/ "/usr/share/doc/"
33
34<directory "/usr/share/doc">
35    Options Indexes MultiViews FollowSymLinks
36    AllowOverride None
37    Order deny,allow
38    Deny from all
39    Allow from 127.0.0.0/255.0.0.0 ::1/128
40</directory>
41</virtualhost>

PHP

Dynamic websites require PHP. With PHPSysInfo you can view server specifications through your browser using an HTML or XML file. If an application uses too much memory, you can easily troubleshoot it with PHPSysInfo. How you can install other modules, like GD, you'll see further on in this article.

1apt-get install php5
2apt-get install phpsysinfo

PostgreSQL

PostgreSQL is a database, MySQL's big brother, which you can use as well of course. However, in this tutorial I'll only explain PostgreSQL. I'll show how to add an user and a database too. You can use this user to login into PHPPgAdmin, the web interface of PostgreSQL.

1apt-get install postgresql
2# Install databasemanager
3apt-get install phppgadmin
4# Create user
5su - postgres
6psql template1
7CREATE USER tom WITH PASSWORD 'myPassword';
8CREATE DATABASE jerry;
9GRANT ALL PRIVILEGES ON DATABASE jerry to tom;

Pure-FTPd

You can use a database to administrate user accounts for the FTP server (Pure-FTPd). This way you can create FTP accounts on your website too. You can choose `pure-ftpd` or other predefined packages, containing MySQL: `pure-ftpd-mysql`, or in my case, PostgreSQL:

1apt-get install pure-ftpd-postgresql

If you've configured everything right, you can create a table `accounts` in your database, using PHPPgAdmin. And then use this table to create virtual FTP accounts:

1CREATE TABLE accounts (
2    id character varying(30) NOT NULL,
3    group_id character varying(30) NOT NULL,
4    home_dir character varying(200) NOT NULL,
5    username character varying(30) NOT NULL,
6    password character varying(50) NOT NULL,
7    expired boolean DEFAULT false
8);

To make the virtual accounts work, you need a normal account with the right privileges:

1groupadd pureftpd
2useradd -g pureftpd -d /var/www tom
3chown pureftpd:tom /var/www
4passwd tom
5# Enter password

It's advisable to create a backup of the following files. First edit `/usr/local/pgsql/data/pg_hba.conf`:

1local   all     postgres    ident                       sameuser
2local   all     all         ident                       sameuser
3host    all     all         127.0.0.1/32                md5
4host    all     all         ::1/128                     md5
5host    system  system      127.0.0.1 255.255.255.255   md5
6local   all     all         ident                       sameuser
7# host  all     all         127.0.0.1 255.255.255.255   md5 
8
9# host  all     tronix      0.0.0.0 0.0.0.0             md5 
10# host  sameuser all        0.0.0.0 0.0.0.0             md5 
11# host  all     all         0.0.0.0 0.0.0.0             reject

Then `/etc/postgresql/8.3/main/postgresql.conf`:

1listen_addresses = 'localhost'
2# If you want other users to be able to login on your server, 
3# you can enter the ip address of the server, or '*'

`/etc/pure-ftpd/db/postgresql.conf`:

1PGSQLServer     localhost
2PGSQLPort       5432
3PGSQLUser       postgres
4PGSQLPassword   xxx
5PGSQLDatabase   swimmer_zone
6PGSQLCrypt      md5
7PGSQLGetPW      SELECT password FROM accounts WHERE username='L'
8PGSQLGetUID     SELECT id FROM accounts WHERE username='L'
9PGSQLGetGID     SELECT group_id FROM accounts WHERE username='L'
10PGSQLGetDir     SELECT home_dir FROM accounts WHERE username='L'

`/etc/default/pure-ftpd-common`:

1STANDALONE_OR_INETD=standalone

Pure-FTPd saves its settings in small files, the name of the file is the name of the setting and the contents are the value of this setting. You can create these files like below. To give everyone access only to their own home directory:

1echo "yes" > /etc/pure-ftpd/conf/ChrootEveryone

You can enter home directories in the database. The following directive allows you to create them automatically, when they don't exist yet:

1echo "yes" > /etc/pure-ftpd/conf/CreateHomeDir

Use the directive below to check permissions on files or directories:

1ls -l /var/www

If the home directory is created, it also gets the right permissions `group_id` for groups, or `id` for users.