Web Server Setup and Virtual Hosts

Apache

The following sections will vary based on purpose of your server. Use this section if you plan to use Apache as a web server. I will also show you how to setup virtual hosts for hosting several websites on one server. Remember, this is a very simple process and I try to cover all the bases for a typical installation. The commands in this tutorial are as if you are root or a user with administrative permissions. You can su or sudo when appropriate if not using root, which is the better way to do things.

First, let’s get the correct packages installed for a LAMP server. Login as root again and execute the following commands to get the right software installed. This will install Apache, OpenSSL (https), and PHP.

apt-get install apache2 openssl ssl-cert libapache2-mod-php5 php5-cli php5-common php5-cgi

It will download and install these packages (you might need your install disk again). It is going to list some more packages that are going to be installed, then ask if you want to install them. Hit enter or type “y” to install. These extra packages are not bad, they are dependencies, something the main programs rely on the operate properly.

At this point, Apache settings are at the default. The default web page is /var/www/index.html and the settings are stored in /etc/apache2/apache2.conf. Execute the following command to enable Apache2 to start at system boot up.

chkconfig apache2 on

Execute the following command to start the Apache service.

service apache2 start

If chkconfig or service are commands that your server does not recognize, install them by typing…

apt-get install chkconfig

Now change directories to /etc/apache2/ and take a look around.

cd /etc/apache2
dir

There are a few important directories in this folder.

/etc/apache2/sites-enabled/
/etc/apache2/sites-available/

You can edit the files inside of these directories with nano if you need to make further customizations for your web server, but be extremely careful as you are editing system configuration files. Use a command like…

nano /etc/apache2/apache2.conf

You will need to restart the Apache service in order to it to reflect any changes you make in any Apache conf file. You can restart Apache with two commands, they both do the same thing. Some administrators prefer one style of command over another, so its your pick.

/etc/init.d/apache2 reload
service apache2 restart

At this point, your web server is ready to go. Just start placing your web documents (HTML, PHP, etc) into /var/www/ and you can see them by going to the IP address of the server in your browser such as http://192.168.0.100. You may need to modify the permissions of /var/www/ for you to be able to place files there, or just use root. BUT, using root can cause problems, especially when you start modifying permissions for the default Apache folder. This may open up security holes in your Apache web server, so be careful and make sure you know how to set permissions up correctly.

This is another consideration for placing your web files. You can redirect the default Apache web server folder to an alternative location. For example, you can edit the sites-available default configuration file to place the website files default location to /home/username/Public instead of /var/www/. This way you can upload and change files for your website with your own user account, not having to worry about using root to move around files. To do this, change directories to /etc/apache2/ and edit the default configuration file…

cd /etc/apache2/
nano /sites-available/default

Go to the line that reads…

DocumentRoot /var/www/

Change the document root folder path to /home/username/Public where username is your login account name. Hit Ctrl+X to invoke save and then hit “y” to actually save the file and exit. Now just restart your apache2 service as shown above to apply and changes made to the Apache configuration files.

At this point, you will need to place your web files in your Public folder under your account. You can test to see if your web server is showing your files by going to your IP address again as shown earlier. If you just want a quick file to place in your folder for testing, see my PHP Info page. Copy and paste the PHP info code into a new file named index.php. Make sure it is in /home/username/Public.

cd /home/username/Public
sudo nano index.php

Virtual Hosts

There are many ways to implement virtual hosts because it depends on how to want to lock down access to the site directory. FTP or SFTP considerations must be included in your decision making. Here are a few popular ways to implement virtual hosts.

You can use the default directory for Apache and create sub-folders for the HTML and PHP files for each user as shown below.

mkdir /var/www/josh
mkdir /var/www/brad

Depending on what you are going to let your users do, you may need to create directories for each user so they can use CGI scripts. You do not have to do this if you know that the websites hosted on the server will not be using CGI. It is more of a feature or add on for web developers if you are unfamiliar with CGI.

mkdir /usr/lib/cgi-bin/josh
mkdir /usr/lib/cgi-bin/brad

In order for the web server to recognize several websites, we need to change the sites-available default configuration file to reflect multiple websites.

cd /etc/apache2/sites-available
nano default

With this open, we must add the following lines so that the web server knows what to do with website requests for multiple domains. If you have two websites, for example joshuamcdonald.net and bradmclaughlin.net, we will need to insert configurations for each site. It does matter somewhat in which order you place the websites, so you want your primary site to be first. For the most part, all other sites can be placed in whatever order you wish, just as long as your primary site is first. Shown here is a barebones example of a virtual hosts configuration for a website. There are many, many things you can place within the <VirtualHost> tags for special configurations. This example will get the web server responding to requests for the examples joshuamcdonald.net and bradmclaughlin.net.

<VirtualHost *:80 >
 ServerName www.joshuamcdonald.net
 ServerAlias joshuamcdonald.net
 DocumentRoot /var/www/josh
 ServerAdmin josh@email.com
</VirtualHost>

<VirtualHost *:80 >
 ServerName www.bradmclaughlin.net
 ServerAlias bradmclaughlin.net
 DocumentRoot /var/www/brad
 ServerAdmin brad@email.com
 </VirtualHost>

Just copy and paste this into your sites-available default configuration file, and change the necessary parts to fit your scenario.

Now as the server gets requests over port 80, the default web server port, it will determine if the traffic is meant for joshuamcdonald.net or for bradmclaughlin.net. Depending on where the web surfer wants to go, the server will give them the site they are asking for.

With the setup just shown above, you will have to dive into setting permissions for these folders and making sure other users cannot access other user’s files (/var/www/josh can’t access /var/www/brad and vice versa). This would be a mess. Again, tread carefully when setting permisisons and ownership for these directories.

So following the advise shown earlier, you can change the document root for the website to a username’s Public folder. Permissions would not have to be modified and users would not be able to access other user’s information by default. By using the document root at a non-default location, it opens the door for further configuration for things such as chroot jail and secure SFTP transmissions. Shown below is an example of a Virtual Hosts file that is directed towards a user’s Public folder rather than at the default /var/www/.

<VirtualHost *:80 >
 ServerName www.joshuamcdonald.net
 ServerAlias josh.com
 DocumentRoot /home/josh/Public
 ServerAdmin josh@email.com
</VirtualHost>

<VirtualHost *:80 >
 ServerName www.bradmclaughlin.net
 ServerAlias bradmclaughlin.net
 DocumentRoot /home/brad/Public
 ServerAdmin brad@email.com
</VirtualHost>