How to setup a dedicated web server with Ubuntu Linux distribution

Published on: Dec 6, 2015 1:09 am  /   Published by:   /   Comments:

Dedicated web server is something that I should try a lot earlier! I have a couple of personal blogging websites and some adventure sports/travel information based websites built in WordPress. This website is one of those as well. My websites were in a 2GB shared Linux based hosting server provided by the Godaddy. As usual, due to the shared hosting service, I was facing a lot of slowness in website loading. It is not that the page content was loading very slowly. Rather the page waiting time was huge than the actual page loading and rendering time! This generally happens with most of the shared hosting services. This is due to the overhead created by imbalanced server vs. shared users ratio, which eats up the server resources in an unmanaged way. So finally, I decided to migrate my sites to a 2GB cloud-based dedicated hosting server with SSD drive. I choose the Linode provider for the hosting. Now my websites are running comparatively smoother and faster than earlier. So I thought it would be a great idea to share my thoughts here on cloud-based dedicated server benefits. And also thought to share the setup and configuration steps of the server, that may help many people in future, like me 🙂

There are many other renowned cloud-based dedicated web server providers as well, like Rackspace, DigitalOcean, Amazon EC2 etc.

Dedicated web server – why should we consider?

Some of the major benefits of cloud-based dedicated hosting server are:

  • The resources are not shared with multiple users in an unmanaged way. Though full dedicated web servers and cloud-based dedicated web servers are different, still you get the resources that actually you pay for.
  • Fully customizable with lots of extra components that shared hosting providers generally do not allow to install, e.g. Memcached, APC etc. PHP components. You can install GIT version control and many other utilities that you may require for advanced website management.
  • An SSD-based hard drive which makes read/write operations much faster which further ensures faster I/O response.
  • Easily scalable to a higher or a lower plan as per your requirement, with flexible hourly billing accumulation and monthly charge.
  • Properly maintained hardware resources and scheduled server maintenance.

I have switched to the cloud-based dedicated web server for the first time from a shared hosting server. So my thoughts may be a little bit biased :p. But still no one can deny that dedicated web servers are far better than shared web servers. Obviously, if you are not interested towards a low-cost solution! Dedicated web server is much high cost than regular shared hosting.

Now let us continue with the step-by-step process of installing the binaries and configuring the web server.

After logging to the server as root, you may see the notice with locale not set properly. The fix is very simple. Edit the “/etc/environment” file and put the line below at the end of it:

LC_ALL=”en_US.UTF-8″

Save the file, log out and log back in and you should be good. If that doesn’t work, you can try to put these lines below in the “/etc/default/locale” file instead:

LANG=”en_US.UTF-8″
LC_ALL=”en_US.UTF-8″

Setup Apache – MySQL – PHP

First, you need to make sure that you have the most updated package lists and security releases for your Linux distribution. Run the following commands to update your dedicated web server.

sudo apt-get update && sudo apt-get upgrade

This doesn’t actually install the new versions of the software. It downloads the package lists from the repositories. Then it “updates” them to get the information on the newest versions of those packages and their dependencies. These pieces of information are then used to resynchronize the package index files from their sources. So to get the newest versions possible in your dedicated web server as per the version requirements of dependencies, run the above commands.

Install Apache

Install Apache into your server by running the following command.

sudo apt-get install apache2

After installing Apache, you should be able to see the default page for Apache by running your dedicated web server IP address in a web browser. At this stage, the default location of the Apache HTML pages is “/var/www/html/”

default-apache-ubuntu-page

Setup user and virtual host

First, you should create a user, specific for the virtual host. Otherwise, you cannot run the PHP as suPHP. Why you need suPHP? Apache runs as the www-data user. But you will create or modify the files as the user for that virtual host. This may cause file permission issue. So suPHP helps here. The web server runs as the same user that you will use to create or modify files. You can add the user to sudoers group to run different sudo commands as well. But you should not enable SHELL access for users which you will use via FTP. Create the directory first before adding the user to that directory as its home directory.

sudo mkdir /var/www/vhosts/domain.tld
sudo useradd -d /var/www/vhosts/domain.tld -m blogpipers
sudo passwd blogpipers

This will create the user “blogpipers” and will set home directory and password for it. sudo usermod blogpipers -s /bin/false will make sure the user do not have SHELL access enabled.

cd /var/www/vhosts/domain.tld
sudo mkdir htdocs
sudo mkdir logs
sudo chown -Rv blogpipers:blogpipers /var/www/vhosts/domain.tld

This will create the log and htdocs folders within the home directory for the user and set the ownership to the user for it. It is better to have publicly accessible files in a separate folder under home directory. That’s why the “htdocs” directory is created. “logs” is for storing apache raw access log and error log files.

The virtual host configuration file instructs Apache to identify different domains or sub-domains in different directories on the dedicated web server. To set up a virtual host, you need to copy the default virtual host configuration file. You can have one configuration file for all the virtual hosts. Or you can have different configuration files for each of the domains.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain.tld.conf

Now you need to edit the newly created configuration file and update the directory paths etc.

sudo nano /etc/apache2/sites-available/domain.tld.conf

The configuration file would look like below.

<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
ServerAdmin [email protected]
DocumentRoot /var/www/vhosts/domain.tld/htdocs
#php_admin_flag engine off [have this line commented for now]
#suPHP_Engine on [have this line commented for now]
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/sub-one.example.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin" />
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/vhosts/domain.tld/logs/error.log
LogLevel warn
CustomLog /var/www/vhosts/domain.tld/logs/access.log combined
</VirtualHost>

For each separate domain, you can create a configuration file like this. Now to enable this virtual host for Apache, you need to enable this configuration and restart the Apache.

sudo a2ensite /etc/apache2/sites-available/domain.tld.conf
If you get an error here after running the above command, try the same command with the config file name only, without the path. Like the command below.
sudo a2ensite domain.tld.conf

Then restart apache.
sudo service apache2 restart

If you already have the content for the site and you copying the file from some other location (i.e. using wget http://www.old-domain.tld/backup/archive.zip) into your new location, you may need to run the following commands in order to make the file permissions proper.

cd /var/www/vhosts/domain.tld/htdocs
wget http://www.old-domain.tld/backup/archive.zip
unzip archive.zip
sudo chown blogpipers:blogpipers /var/www/vhosts/domain.tld/htdocs -R
sudo chmod g+w /var/www/vhosts/domain.tld/htdocs -R
sudo find /var/www/domain.tld/htdocs -type d -exec chmod 755 {} +
sudo find /var/www/domain.tld/htdocs -type f -exec chmod 644 {} +

Manage Apache Modules

Apache has many modules that can be enabled or disabled. You can check available modules by issuing this command.
ls /etc/apache2/mods-available

To check already enabled modules, run this.
ls /etc/apache2/mods-enabled

In general, we will need the rewrite module, e.g. for a WP site. So let’s enable that and restart Apache.
sudo a2enmod rewrite
sudo service apache2 restart

You can disable a module as well. Need to restart Apache for the change to take effect.
sudo a2dismod mod_name
sudo service apache2 restart

Install PHP

Now install PHP as Apache Module mode.
sudo apt-get install libapache2-mod-php5

You can then install additional PHP modules as you require them. You must need to restart Apache in order to get the effect.
sudo apt-get install php5-mysql php5-memcache php5-memcached php5-mcrypt php5-sqlite php5-json php5-cli php5-curl
sudo service apache2 restart

You can now place an “info.php” file in the “/var/www/vhosts/domain.tld/htdocs” directory and edit the “info.php” file content with
<?php phpinfo(); ?>
and run the file as “http://www.domain.tld/info.php”.

Install and Setup suPHP

As mentioned above that there might file write permission issue as the Apache user and FTP user will be different, in this case, “www-data” and “blogpipers”. So if we use “suPHP” module, the Apache will be able to run PHP with “blogpipers” user under that user’s home directory. This will ensure no file permission related issue occurs.

sudo apt-get install suphp-common libapache2-mod-suphp

Now in the “/etc/apache2/sites-available/domain.tld.conf” file remove the commenting for the following line and restart Apache.
php_admin_flag engine off
suPHP_Engine on

sudo service apache2 restart

Install MySQL & PHPMyAdmin

Just install MySQL as other services. Then run the installation script command. And at the end, secure MySQL server by changing the root password for MySQL. When prompted enter a secure root password. You can answer yes to all the questions asked during this step.
sudo apt-get install mysql-server
sudo mysql_install_db
sudo mysql_secure_installation

If you want to install MariaDB instead, you need to run the following commands.

sudo apt-get install software-properties-common
apt-key adv --recv-keys –keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/10.1/ubuntu trusty main'

sudo apt-get install mariadb-server
sudo mysql_secure_installation

Now to install phpMyAdmin, follow this article How to setup and configure phpMyAdmin on Ubuntu.

 

layers