Apache For Beginners


Bird ImagePhoto by John Duncan on Unsplash

Setting up a website might be easier than you think. Using Apache is one of the simplest ways to go about it. For those not familiar with it, Apache is a web server software package available on Linux systems. It is one of the most popular web servers on the market, and for good reason. It’s free and completely open-source. But it’s also feature-rich, simple to set up, and easy to work with. Let’s delve into how to set up your website using Apache.

Installing Apache

First of all, if you don’t already have Apache on your machine, you’ll obviously need to install it. To install apache2 on a Linux machine, run these two commands:

sudo apt update
sudo apt install apache2

Running these commands will automatically install all the required packages for you. If you made it through the installation process without encountering any errors, your server should be ready. To test its readiness, use your browser to go to this site: http://LOCALHOST or http://YOUR_IP_ADDRESS. You should see a page that looks like this:

Apache Default Page

Important Files And Directories

Now that you have Apache installed, let’s review some of the more important files and directories of an Apache server!

Content directory

Apache comes with a basic site, the “It works!” page above. It is located at /var/www/html. This directory contains actual web content that you can serve to your users. If you are only hosting one domain on your server, you can add content straight into this directory.

Server configuration directory

All the files that configure Apache are located in /etc/apache2. The main configuration file is /etc/apache2/apache2.conf. It contains some global configurations for your server and is responsible for loading other configuration files. A file named /etc/apache2/ports.conf specifies the ports that Apache will listen on. By default, Apache listens on port 80, and also on port 443 if SSL is enabled. A subdirectory callled /etc/apache2/sites-available/ stores the site-specific configuration files for each virtual host. That’s different than the /etc/apache2/sites-enabled/ subdirectory, which stores sites that have been enabled. When a site is enabled, Apache will read the files in this directory to complete that site’s configuration.

Server logs directory

Apache stores its access logs in /var/log/apache2/access.log and its error logs in /var/log/apache2/error.log.

Some Basic Commands

The following are some basic commands that you will need in order to manage your server. You can enable your Apache site by using the command a2ensite (Apache 2 enable site) and disable it by using a2dissite (Apache 2 disable site).

sudo a2ensite YOUR_DOMAIN.conf
sudo a2dissite YOUR_DOMAIN.conf

YOUR_DOMAIN is the name of your site’s virtual host configuration file, minus the “.conf” extension. We’ll see an example of this below. To start or stop your Apache server, you can run:

sudo systemctl start apache2
sudo systemctl stop apache2

You can also restart your server with one command using. To do so, use:

sudo systemctl restart apache2

Sometimes, when making configuration changes, you don’t want to spend the time and effort it takes to restart your server. In these situations, instead of restarting the server, you can try reloading it by using the following command.

sudo systemctl reload apache2

Setting Up Virtual Hosts

What if you need to host multiple websites on the same server? Virtual hosts enable you do that. It’s a method for allowing multiple domain names to use the same physical server and share computational resources. If you’ll be hosting multiple sites on the same server, it’s a good idea to not store all your web content in the same location (/var/www/html). Instead, you should create a separate directory for each of your domains. Let’s say that we are creating a site with the domain name “mysite.com”.

sudo mkdir /var/www/mysite.com

You can then fill web content for that domain in the domain-specific directory. The following command creates an empty index file for your site.

sudo touch /var/www/mysite.com/index.html

The contents of that index file could be as simple as the following:

<html>
   <body>
       <h1>This is your index page :) </h1>
   </body>
</html>

For Apache to serve your content, it will need configuration files for this specific domain. So you need to create a configuration file named “YOUR_DOMAIN.conf” in the “sites-available” directory. You can do so using the following command:

sudo touch /etc/apache2/sites-available/mysite.com.conf

Apache comes with a default configuration file located at /etc/apache2/sites-available/000-default.conf. You can copy the contents over and make modifications as necessary.

<VirtualHost *:80>
   ServerAdmin admin@mysite.com
   ServerName mysite.com
   DocumentRoot /var/www/mysite.com
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The “DocumentRoot” line specifies the location of the web content used for this site. The “ServerName” line specifies what users should type in the address bar to reach your site. Finally, you should put your email address in the “ServerAdmin” line so that your users will have someone to contact when they experience issues with the site. Now you are all done configuring your site! Simple, right? When your site is ready, you can enable it by running:

sudo a2ensite mysite.com.conf

It’s also a good idea to disable the default site that came with Apache. Use this command to do that:

sudo a2dissite 000-default.conf

Finally, check for configuration errors and reload Apache to finalize your changes.

sudo apache2ctl configtest
sudo systemctl reload apache2

Hurray! Now when you browse to http://YOUR_DOMAIN, you should see an HTML page that says:

Index Page

Your site is now running and ready to serve the Internet!

Conclusion

Apache is an easy-to-use web server that is widely adopted in the industry. As evidence of that, in just a few minutes today, you learned how to set up your website using Apache. You’re off to a great start, and where you go from here on the web is up to you!

Vickie Li