How to setup a web server on a virtual private server

by Veesp 21 June 2016 Comments
How to setup a web server on a virtual private server

Are you disappointed with functionality of a shared hosting? Does your growing web-project demand for more resources? The optimum solution for young and prospective websites, gaining ground on the net, is the switch from shared hosting to virtual private server.

VPS/VDS is a Virtual Private (Dedicated) Server. As a rule, this service includes access to a part of physical server hardware resources (configuration and performance of which depend on the plan you choose). It is a real quantum leap in the field of online resource development.

VPS/VDS hosting combines flexible dedicated server configuration, individual IP address, ports, ability for user to choose a software and means of developing. All the services are available for a reasonable price, not much higher than the price of a shared hosting.

Still, leasing a virtual server requires your own configuration and software optimization. However, that should not discourage you - the devil is not so black as he is painted. This article is a guide through the installation the necessary software and maintenance of stable web server operation. All the settings below are valid for the Linux Debian platform – one of the most secure operating systems, providing a high performance with minimum resources consumption.

Optimum VPS configuration

The major problem of any shared hosting is that one physical server may contain 50-100 websites on average. The idea is that the load is distributed evenly through the day, as long as users don't visit sites simultaneously. In theory each site should successfully cope with several thousand users per day, nevertheless, in practice everything often goes sideways.

Usually popular online resources occupy RAM and some web applications engage the most part of the computing resources, as a result loading time of slack projects increases gradually. In effect, during the peak time server simply fails to cope with higher load and the majority of the websites and web applications on the server become not reachable at best. In the worst case, none of them are accessible.

However, if you lease a virtual server, this isn't a problem for you anymore. You have a certain amount of hardware resources at your disposal that none else can use. Therefore, even at peak times your site will work stably, and during the slow periods no other application would be able to occupy the RAM or CPU time. The question is which configuration would fit you best?

Small project

If you switch from a shared hosting or just set up a project, which has high security requirements, the minimal characteristics are enough:

  • 512 MB of RAM;
  • 5 - 10 GB of disk space;
  • 1,5 - 2 GHz CPU.

The resources are enough to cope with up to 10 thousand users a day. Moreover, it is possible to reach the number of 20-25 thousand users, if webmaster makes optimization correctly. Further, as the number of users will increase, you may broaden the potential of the system by changing your plan for more expensive one.

Rapidly growing website, forum or small online-shop

  • 1 - 2 GB of RAM;
  • 20 - 30 GB HDD;
  • 2 cores of 2-2,5 GHz.

It is a good variant for popular websites, news portals, blogs with the audience of 20-30 thousand visitors a day.

Thriving website, small game server (Minecraft, Mu, Lineage, etc.)

  • From 4 GB of RAM;
  • 30-50 GB HDD;
  • 2 - 3 cores of 3 - 3,5 GHz.

The best solution for successful web projects with the audience of about 50 thousand users. The hardware is enough to process multimedia content and develop online-game servers.

The characteristics are valid for Linux Ubuntu and Linux Debian distributions. They are particularly stable and provide high performance with the minimum resources consumption.

Connection to a server via SSH

To connect to a VPS SSH protocol is used. SSH (Secure Shell) is a secure protocol purposed for remote control of an operating system, software and file transfer. SSH uses reliable encryption algorithms and provides secure connection to a server.

To get the SSH protocol work you need a server, installed on a host, and a client. Control over the VPS/VDS server is implemented through the command line interface.

There are quite a few client applications suitable for various operating systems. The most simple, reliable and popular among them are Bitvise SSH Client and PuTTY. In the case below we'll use the second one.

In the «Host Name» field you should enter the IP-address of the virtual server, switch the "connection type" radiobox to SSH, value of «PORT» field should remain 22, in «Saved Sessions» choose random session name. Now you should press «OPEN» at the bottom of the window.

After that you'll see the console window, looking similar to the Windows command line. The system will ask you to enter a login – root by default.

Pay attention! «Root» stays for the super user with the superior privileges in Linux system, which is the same to «Administrator» in Windows system. Further you'll be able to create a separate user with limited privileges for each website.

Next you should enter the password (in PuTTY you may simply copy it with a right mouse click) and then press Enter.

Now connection is established!

For security purposes you may add a user with the limited privileges before continue. To do that you should enter the command:

adduser john_doe

where john_doe may be changed for any other username.

In the dialogue box that will appear, you should enter a new password. You may also fill in the fields with the additional information about the user.

Then you should allow the new user to implement commands of the super user. To do that you'll need a sudo utility.

Pay attention! Sudo is an utility that allows a user to administer a system with root privileges. It is activated by entering "sudo" at the beginning of a command.

Installation of any software in Linux operating systems always starts with packet index updating. During the packet index updating your system accesses repository (where the main part of software for your distribution is held) and "checks out" the latest versions of the software and interdependence of software packages.

You should begin with the command:

apt-get update

Once the index is updated, you should install the sudo utility:

apt-get install sudo

Then allow john_doe to implement the sudo command:

usermod -a -G sudo john_doe

Now you can enter the system and continue configuring it as the new user. Enter the command:

ssh john_doe@хх.ххх.xx.xx

where хх.ххх.xx.xx is the IP-address of the server.

You will be asked whether you want to make a connection. In the console you should enter yes and press Enter .

There you may see some useful commands for work with the command console:

  • ls -lha - show a list of files, downloaded to a server, and data about their size and access rights;
  • cd [directory name] - go to the chosen directory;
  • mv - change the name/transfer;
  • rm - delete;
  • cp - copy;
  • chmod - settle access rights to the file or directory;
  • cat - show the file;
  • mc - start the integrated file manager, similar to the Norton Commander;
  • mkdir [random_name] - create a new directory.

Now you are ready to start installation of the necessary software.

LAMP installation and configuration

In order to launch and configure a web server you will need to install basic software: LAMP (Linux+Apache+MySQL+PHP). You can start the installation of the LAMP set and phpMyAdmin panel by entering:

sudo apt-get install apache2 apache2-doc mysql-server mysql-client php5 php5-mysql libapache2-mod-php5 phpmyadmin

Then you'll see the dialogue box, where you will be asked to confirm the installation of the chosen files. To confirm enter Y and press Enter .

In the next dialogue box you should create a password for MySQL data base (later you will be able to change the password):

To confirm enter the password once again:

Then the system requests a server type to make an automatic configuration of phpMyAdmin. You should choose apache2:

In the next dialogue box you'll have to confirm the standard settings for phpMyAdmin and automatic creation of a system data base (press Y and Enter ):

Create a password for phpMyAdmin and repeat it:

Now you need to correct the Apache configuration. Open the configuration file:

sudo nano /etc/apache2/apache2.conf

Go to the end of the file and enter on the new line:

ServerName localhost:80

To quit the editor press Ctrl+X and confirm the changes by pressing Y and Enter .

How to add websites to VPS: Apache and DNS configuration

Apache allows creating an endless number of virtual hosts, which makes it possible to link several sites to a single IP-address. Nevertheless, you shouldn't forget about hardware resource limits. The process of hosting for several sites includes a number of steps.

Step 1. Creation of separate websites' directories

First of all, you should create the directories, which Apache will access. All the files of your websites and web applications will be held there. In the command console you should enter:

sudo mkdir -p /var/www/mysitename1.org/public_html
sudo mkdir -p /var/www/mysitename2.org/public_html

mkdir is a command which creates a directory (folder); «-p» parameter is defined for recursive folder creation: firstly, «mysitename1.org» directory is created; secondly - «public_html» directory, where the content of the web resources, you are going to place, will be held. «/var/www/» is a path to create new directories. Names of the folders may be random.

Please, note, that Apache has access only to the /var/www directory and the folders and files within.

Step 2. Setting file permissions

All the permissions for directories belong to the root-user by default. It is necessary to define the owner(s) for security purpose. To do this you should enter:

sudo chown -R john_doe:john_doe /var/www/mysitename1.org/public_html
sudo chown -R john_doe:john_doe /var/www/mysitename2.org/public_html

where john_doe is the name of a user, who is presumed to have access to public_html directories, where website's files are held.

To edit access permissions enter:

sudo chmod -R 755 /var/www

Pay attention! Access permissions in Linux systems are set in numbers for all users: file-/directory-creator, owner's user group and other users.

For example, 755 means:

Owner Group Others
7 5 5
All rights: reading, executing and writing Reading and executing Reading and executing

In the following table you may see the variants of entitling access permissions:

Value Access rights to the file Access rights to the directory
0 No No
1 Executing Reading files, viewing file properties
2 Writing No
3 Writing, Executing All, except viewing list of files
4 Reading View file names
5 Reading, Executing Access, Reading
6 Reading, Writing Access, Reading
7 Full rights Full rights

Step 3. Test pages

In order to verify server configurations, it is necessary to create test pages. Here is an example for a first website. This procedure is similar for all the resources that are to be installed on the server.

Firstly, you should create index.html:

nano /var/www/mysitename1.org/public_html/index.html

Within the file you should create a simple html document:

<html>
  <head>
	<title>Welcome to mysitename1.org!</title>
  </head>
  <body>
	<h1>Virtual hosting works!</h1>
  </body>
</html>

Then you should save the document.

You'll have to do the same procedure for all other sites. Still, you can save time by simply copying the contents of the first document into the second website directory:

cp /var/www/mysitename1.org/public_html/index.html /var/www/mysitename2.org/public_html/index.html

Step 4. Apache virtual hosts configuration

Right out of the box web server Apache has a file default with virtual hosting configuration. You only have to edit the source document.

Firstly you should copy the file:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysitename1.org.conf

Then you should edit it with your root-privileges:

sudo nano /etc/apache2/sites-available/mysitename1.org.conf

Next the file contents should be changed with the following minimum configuration:

<VirtualHost *:80>
    ServerAdmin feedback@mysitename1.org
    ServerName mysitename1.org
    ServerAlias www.mysitename1.org
    DocumentRoot /var/www/mysitename1.org/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now you can save the document and do the same to the rest of domains.

Step 5. Turn on the hosts

In order to apply new configuration you should enter:

sudo a2ensite mysitename1_org.conf
sudo a2ensite mysitename2_org.conf

Then restart the server to activate the changes:

sudo service apache2 restart

Step 6. NS domain records configuration

Another important point is changing NS records for your domains. The easiest way is to link a domain name to the IP-address of your VPS at Domain Name Services Company. To do this you'll have to park a domain to the service's DNS servers and in the DNS-zone control section create three A-type records (or ones of any similar type) named: «*», «@» and «www». In the «IP» field you should enter the VPS address. Please, pay attention! Changing of the NS records may take up to 24 hours.

In case you use a test unregistered domain name, it is necessary to change «hosts» file on the local computer, so that you could test workability of the configured web server. In Windows system this file has the following path: «C:\Windows\System32\Drivers\etc».

You should open the file in Notepad with administrating privileges. At the end of the file you should add IP address of the server and test domain names of your websites:

хх.ххх.хх.ххх mysitename1.org
хх.ххх.хх.ххх mysitename2.org

Save the document. Here хх.ххх.хх.ххх stands for the VPS IP-address.

Now the web server is configured!

If you enter mysitename1.org into your browser address bar, you'll see the following text: Virtual hosting works.

It means that everything is ready for downloading and installation of your working projects.

Subscribe for our newsletter
to get the latest news & updates!
Visa
Mastercard
Paypal
Bitcoin
American Express
JCB
Diners Club