Welcome

Apache Site Management

The apache web server was developed in a way to easily support multiple websites living on a single server. The way that you manage these sites on an Ubuntu installation is actually quite easy if you follow the structure that is created for you during the installation. This might mean not following certain online tutorials exactly. If there is some tutorial that you are following, try to adapt the tutorial to fit the concept of “sites”.

The /etc/apache2 folder contains a file called apache2.conf which contains the main configuration file that is loaded. Older versions of apache and books for web server management recommended making changes to this file directly but with the exception of some global settings, it is likely the case that you won’t need to change anything in this file ever. The magic line in the apache2.conf file appears towards the end:

Plain Text

This line indicates that everything found in the /etc/apache2/sites-enabled folder will be loaded as part of the apache configuration. The best way to add additional sites to your web server is by adding and removing files from this folder (but it should be done in an indirect way).

Another other important section in the apache2.conf file is:

Plain Text

This section tells the apache server that: 1. It is allowed to follow symbolic links, 2. Ignore all .htaccess files (I’ll speak specifically about this later), 3. Allow access to the files. If you need to change the location where the HTML files are kept you could make a change here but once again, I usually make changes in the site configuration files.

Default Site

There is a configuration file already installed called /etc/apache2/sites-enabled/000-default.conf that serves up HTML files from /var/www/html. The default installation looks like this (I’ve dropped the comments from this file to make it smaller):

Plain Text

This “000-default” website is the *catch all* in case no other sites are configured properly. If we only ever plan on having a single site configured on this server and no DNS named website, you could disable this site, but most servers would probably leave this site enabled with a custom message or instructions to shut down the communication with the client.

On my servers, I usually change the Ubuntu default message to be something else like “default site” so that if I see it I know something is wrong, but you could also write up a page that looks like a “404 file not found”. I have also seen comments on line where people have written code to actually shutdown the connection if this site is ever served up since it could be somebody looking for a security hole. Once we have multiple sites working on our server, we will see better examples of the use of the 000-default site.

One Site at a Time

If our goal is to have just one website on our server, we could go in the /var/www/html folder and simply take over that folder. I have done this on some servers that I have managed in the past but as you start to add things like reverse proxies and applications that use different server side programming languages I often found myself regretting the decision of using the 000-default site and usually had to do a bunch of reconfiguration.

Step 1 – Create a new folder in /var/www for your HTML

You will need a new folder to hold your website code. This folder needs to be in /var/www along side of “html”. Inside this folder you can put your HTML files along with any CSS, JS, and image files (and of course those can be inside of other folders).

The ownership of the files in /var/www after the initial setup is root. This means that you will need to be root in order to create any of the files and/or folders. This is not the ideal setup but since we are just learning about managing sites, we will leave this for now. I will create a section about securing apache in a later post.

Step 2 – Create a new Configuration File and Enable

Go to the /etc/apache2/sites-available folder and create a new .conf file for your site. It is quite important that you do not go to the sites-enabled folder but rather to the sites available. You could either create a new file from scratch or you could start by copying the configuration file of another site. You of course will need to make sure that the DocumentRoot folder has been configured to point to your new folder under /var/www.

Before going any further look at the files in the “/etc/apache2/sites-available” and the “/etc/apache2/sites-enabled” folder. Chances are that you will see the 000-default.conf in the sites-enabled folder and 000-default.conf along with your new site in the sites-available. If you look closely at the sites-enabled folder you will also see that the file 000-default.conf is not actually a file but rather a symbolic link to a configuration file in sites-available. To create an enable a new site on Ubuntu, you should put your configuration file into the available sites and then create the symbolic link using the a2ensite command.

After the .conf file has been created we can then “make it enabled” by using the command “a2ensite“. If you enable the site, you should then see the configuration file in the enabled side. For example, create a site named blue.conf in the sites-available and then enable it with a2ensite blue.conf.

Step 3 – Verify The Configuration

Before we actually make apache look at the new configuration files we should verify the configuration file. The last thing that you want to have happen on a live server is to make a simple spelling mistake that brings down all of the sites. The configtest may not catch all problems but it will do a good job at finding at least some things.

To run the configuration test you can type “apachectl configtest“. Mistakes that are discovered here should probably be corrected before actually trying to load the configuration files.

You could skip this test but it is only one extra command and will save you the embarrassment of phone calls or emails from people asking if the server is down all because you said “DocumentRiit” while typing.

Step 4 – Reload the Apache Configuration

Although you might have perfectly fine configuration file created, enabled and verified the apache server of course is likely currently running and knows nothing about your new configuration file.

In order for apache to learn about your configuration you will need to reload the configuration. The best way to do this is by running the command “systemctl reload apache2“. If you see no errors then there is a chance that everything will be okay but you of course still need to test your sites with a browser.

You may find lots of websites suggesting that you need to run the command “systemctl restart apache2” after making changes. The restart operation is the same as stopping apache then starting it again. The net effect of course is that apache2 will start with the new configuration changes. While restarting is certainly one way to read a new configuration it is going to make your server more busy than simply a reload and most of the time restarting the apache2 process is not actually necessary, a reload will suffice in just about every case.

Multiple Enabled Sites

What if you enable multiple sites at the same time? When you request a page from apache, it searches through all of the VirtualHost entries in the order that the configuration files were loaded (alphabetically) to see what configuration matches the closest to the requests. If we enable our website along with the default website with no other identifying features like a server name we will end up seeing the default because the name of the file is 000-default.conf which appears first.

In order to support multiple sites at the same time we need to have separate IP addresses, separate ports, or separate DNS entries for each of the sites. These will be covered in the next few posts.

Troubleshooting

Step 1 – Check Sites-Enabled

One of the first checks that I make is to see what is in the ‘sites-enabled’ folder. Use the “ls -l” command to find out.

Using the “ls -l” will also reveal any sites configuration files that were placed directly into the /etc/apache2/sites-enabled rather than into the sites-available followed by the symbolic link via a2ensite. While putting configuration files directly into the sites-enabled folder will work, it will make you server more difficult to manage. If you do have a configuration file in this sites-enabled, you should move it to the sites-available and then use a2ensite to setup the symbolic link.

Step 2 – Reload the Configuration

In my course, a large percentage of the problems that students experience is because they simply didn’t reload the configuration file (usually they attempted to reload it but made a mistake typing the command and didn’t notice the error message, or they made changes after reloading and didn’t reload a second time). The usual conversation goes like this:

Student: “Sir, my website isn’t working”

Me: “Did you reload the configuration”

Student: “Yes I did that already”

Me: “Reload the configuration file with me watching”

Student reloads and everything works.

Step 3 – Check the Status

Check to see if the apache service is running with ‘systemctl status apache2‘.

Step 4 – Check the Log Files

In particular the /var/log/apache2/error.log file for signs of problems.

Leave a Reply

Your email address will not be published. Required fields are marked *