After you have created your first VPS, your VPS provider should give you the credentials for logging in through ssh. For Linux users you can just type into the terminal:

 ssh [user]@[your-vps-ip]

For Windows user you can download Putty an application that can be used for ssh. Now connect to your VPS through ssh. I assume you are now using the root user.

Update/Upgrade Server

Make sure that your server is updated. Type in:

apt-get update
apt-get upgrade

Change “root” Password

This part is optional, if you want to change the “root” password to something that you will be able to remember then use the below command otherwise move on to “Secure ssh”

passwd

Secure ssh

To secure our ssh, we will disable the root login and add a new user. This way it will be harder for someone to login into our server since most likely they will try to login using root.

Add New User
adduser [new-user]

It should add your new user and will ask for password and other informations.

Add The New User On Sudoers

Type in the terminal:

visudo

this will open the sudoers file. Just add this line:

[new-user]    ALL=(ALL:ALL) ALL

replace [new-user] with the user you just created. Add it after this:

 # User privilege specification
 root ALL=(ALL:ALL) ALL
Disable Root Access

Now to disable the root access you will need to edit the configuration file of ssh located in “/etc/ssh/sshd.conf”.

nano /etc/ssh/sshd.conf

It should now open the file find the line “PermitRootLogin yes” and change it to “Permit RootLogin no”. Add a new line into the bottom “AllowUsers [new-user]”, [new-user] should be the one you created earlier. Press “Ctrl + o” then “Enter” then “Ctrl + x”, now we will need to restart the ssh server. You can type:

/etc/init.d/ssh restart

or

service ssh restart

After that root login in ssh should now be disabled and you can only login using the new user you have added earlier.

Add Swap File

Swap file is where the OS store temporary the memories from RAM that arent being used currently. This avoids some of your application to crash when there are no RAM left. For example Apache and MySQL sometimes uses a lot of RAM on startup and later discards them or on a spike of visitors a temporary drastic increase on RAM consumption can also happen. Using a swap, an OS can give more RAM to these applications while moving those memories that arent currently active into the swapfile. Usually Linux allocates a partition of an HDD into a swapfile upon new installation/reformat but since we are using a VPS it is more likely that there are no swap partition so instead we will create a swap file. You can check your current swap by typing:

 free -m

you should see something like this:

 total used free shared buffers cached
Mem: 497 447 49 0 40 158
-/+ buffers/cache: 248 248
Swap: 511 1 510

Check the fourth line says it has a swap of 511MB with 1MB used and 510MB free. If you dont have a swap these 3 values should be 0.

To create the swap file, first we need to create the file that we are going to use for swap by

dd if=/dev/zero of=/swapfile bs=1024 count=512k

count is the size the file that dd command will create. Above is set to 512MB if you want 1GB then set it to 1M. Be reminded though that using a large swap file can have a backward effect.

Then make the file we just created as a swap file

mkswap /swapfile

then mount it

swapon /swapfile

Your swapfile should now be ready to be used. You can check it again by typing

 free -m
Mounting Swap On Startup

Edit the file /etc/fstab

nano /etc/fstab

then add this line to the bottom

/swapfile       none    swap    sw      0       0
Swap Priority

We need to set the swap priority to 10 since currently it should be on -1. Leaving it to -1 can be a cause of thashing where in the OS will always use the swapfile. Since HDD is much much more slow compared to RAM this can cause a drastic slow down of your system. To avoid that we will set the priority to 10 so that our swapfile will act as an emergency memory location.

 echo 10 | sudo tee /proc/sys/vm/swappiness
 echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

That should be it, now your VPS should be ready to go. You can set it up now as a Web Server, Email Server or both.

 

Looking for cool VPS provider? Try Linode(a referral link), they are currently my VPS provider.