Install and Configure Apache Web Server On Linux Mint 18.2

As installations go, installing Apache on Linux Mint is a breeze. (Configuring it, of course, is substantially more difficult.) To install Apache, type this command at a Terminal window or a command prompt:

sudo apt-get install apache2

(Technically, you’ll be installing Apache 2, the latest version.)

Enter your password to authenticate, follow the default prompts, and apt will download and install the Apache web server for you.

And that’s it! Apache should now be working. To test it from the web server itself, go to a web browser and visit the address http://127.0.0.1. You’ll remember that this is the “local loopback” address, basically the IP address the local computer uses to refer to itself. Alternatively, you could test it from another computer on the same subnet. For instance, if you installed Apache on a computer with an IP address of 192.168.1.100, you could test it by going to another computer on the same subnet and visiting http://192.168.100 from the web browser.

Regardless, if Apache is working properly, you should see the default Apache/Linux Mint web page with the Linux Mint logo in the upper left-hand corner.

Apache is now operational. In the next section we’ll talk a little bit about configuring Apache.

 

BASIC APACHE CONFIGURATION

 

Apache configuration can get extremely complex, depending on the scale of the website involved. A full overview of Apache configuration is way beyond the scope of this book – indeed, dozens of excellent books have been written on the topic. However, here we can take a brief look at Apache configuration.

Apache’s chief configuration file is /etc/apache2/apache2.conf. (In some Linux distributions, it’s /etc/apache2/httpd.conf, but Linux Mint uses /etc/apache2/apache2.conf.) You can look around in the file using vi, cat, or less, but it’s generally a good idea to change things only if you know what you’re doing. And as always, make sure to create a backup copy of a configuration file in your home directory before you alter it, in case something goes irretrievably wrong.

Linux Mint stores your actual web files (the HTML files that make up your website) in the /var/www/html directory. After an installation of Apache, there’s only one file in that directory – the index.html file, which contains the “it works!” message we saw earlier. Otherwise the directory is empty. You can put your own HTML files in here. Coding HTML is beyond the scope of this book, but remember that you’ll need to create your own index.html file once you build your own site.

However, loading HTML files into the /var/www/html directory will only allow you to create a static website. If you want a dymanic website, once that updates content, you’ll need a database backend – and we’ll discuss that in the next chapter. In the next section of this chapter, we’ll show you how to set up Apache to use virtual hosts.

 

VIRTUAL HOSTS

 

As we’ve discussed in the previous section, it is easy to set up a simple web server with Apache. However, you might need a more complex web server, one that can host multiple separate websites, rather than subdirectories within a larger website.

How do you do this?

Apache has a feature called “virtual hosts” that lets you run separate websites, with completely different domain names, on the same physical server. Fortunately, implementing virtual hosts for Apache is actually quite simple to do. In this example, we’ll show you how to set up a new website called “examplename.com” on an Apache web server.

First, you’ll need to set a location for your new web site’s files. Apache, you might recall, by default stores the web files in /var/www/html. For this example, create a new folder for the website with this command:

sudo mkdir /var/www/html/w2

Next, you’ll need to create a configuration file for the new website in the /etc/apache2/sites-available directory. Fortunately, that directory contains a default file you can simply copy and use as a template with this command:

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

(Make sure to include .conf extension. Linux Mint 14.04 Trusty Tahr and above run Apache 2.4, which requires it.)

The next step is to edit the new configuration file with the correct settings. You can do this using the vi text editor:

sudo vi /etc/apache2/sites-available/examplename.conf

(We discussed how to use vi back in Chapter 6.)

Once editing the file with vi, you’ll need to make a few changes.

Change the “Document Root” directive from /var/www/html to the proper location of your new website, in this case, /var/www/html/w2.

Also change the “Directory” directive from /var/www/html to /var/www/html/w2.

Finally, under the line that begins “ServerAdmin”, add a new line for the new website’s domain name. For our website named “examplename.com”, add a line like this:

ServerName examplename.com

Save changes to the configuration file, and then exit vi.

Now you’ll need to modify Apache to display the new domain names to web visitors. Fortunately, you can do this with the a2ensite command:

sudo a2ensite examplename.conf

(Note that “examplename” would change depending upon what you named your configuration file.)

One final step – restart Apache to force it to re-read its configuration files and start serving the new website:

sudo /etc/init/d apache2 restart

Make sure you have some sort of index.html file in /var/www/html/w2, and the examplename.com website should now be working.

Was this post helpful? These books might be useful: