The next step after setting up and securing VPS server is installing the software stack that is required for hosting websites. I am going to install Apache as web server, you can follow or install Nginx.
Installing Apache is easy just enter the below command. I have disabled root login and using a user account with admin privileges to manage server, that is why sudo is appended in the command line. If you are using root login then don’t enter sudo.
sudo apt-get install apache2
After installing Apache you need to change some settings for better performance. Apache2 configuration is stored in apache2.conf file, previous apache versions used httpd.conf as config file, that is deprecated in apache2.
Open apache config file for editing with the following command.
sudo nano /etc/apache2/apache2.conf
You need to change the values for MinSpareServers, MaxSpareServers, MaxClients and MaxRequestserChild according to your needs as a starter. Alternatively you can use .htaccess files to override apache configuration directives.
After you are done save changes to the file by pressing Ctrl + X and pressing Y.
Restart Apache for the change in settings to take effect.
sudo service apache2 restart
Next you need to create directories for hosting files and need to create vhost files for domain names. We will see that later.
vhosts files can be created with the following command
sudo nano /etc/apache2/sites-available/example.com
A typical vhost file for individual domains will look like this. Change the directory paths to domain name to reflect your setup.
# domain: mydoamin.com # public: /var/www/hosts/mydomain.com/htdocs <VirtualHost *:80> # Admin email, Server Name (domain name), and any aliases ServerAdmin email@example.com ServerName www.mydomain.com ServerAlias mydomain.com # Index file and Document Root (where the public files are located) DirectoryIndex index.html index.php DocumentRoot /var/www/hosts/mydomain.com/htdocs # Log file locations LogLevel warn ErrorLog /var/www/hosts/mydomain.com/log/error.log CustomLog /var/www/hosts/mydomain.com/log/access.log combined </VirtualHost>
After creating the file save with Ctrl+x and by pressing y.
Now enable the site using this command
sudo a2ensite mydomain.com
Restart apache for the changes to take effect.