Apache is one of the internet's most favoured web servers, serving over half of all active websites. While various web servers are available for content delivery, understanding Apache's operations is beneficial due to its widespread usage.
Setting up Apache for basic functionality is simple. This guide will lead you through the installation and configuration process of the Apache web server on the Ubuntu operating system.
Prerequisites:
Ubuntu or Debian OS
User with root or sudo privileges
Note: The steps outlined in this article have been demonstrated on an Ubuntu 20.04 LTS system.
Installing Apache
Step 1: Update
To ensure we install the latest version of Apache2, we need to update the system repository index first. Launch the Terminal using the Ctrl+Alt+T shortcut and execute the following command:
sudo apt update
It's important to note that only authorized users can install, remove packages, or update from the Linux system.
Step 2: Install Apache2
In this step, we'll proceed to install the Apache2 web server. Execute the following command in the Terminal:
sudo apt install apache2
You might be prompted for confirmation with a Y/n option. Press y and then Enter to proceed. The Apache2 web server and all its dependencies will be installed on your system.
After installation, confirm the Apache server version by executing:
apache2 -version
Step 3: Firewall Setup
Next, we must open specific ports on our system to enable external access to Apache. Initially, let's list the application profiles Apache requires access to. Execute the following command to accomplish this:
sudo ufw app list
Here, you can observe various Apache profiles.
We'll use the highly restrictive 'Apache' profile to allow network activity on port 80.
sudo ufw allow 'Apache'
Now, check the status to confirm that Apache is allowed in the firewall.
sudo ufw status
Configuring Apache web server; Verifying Apache service
Before proceeding with configuration, confirming if the Apache service is running is crucial. To do this, execute the following command in the Terminal:
sudo systemctl status apache2
From the above output, it's clear that the Apache2 service is active and running.
Another way to confirm if Apache is running properly is by requesting a web page from the server. To do this, find your IP address using the following command:
hostname -I
When you obtain your server's IP address, enter it into your browser's address bar:
You'll encounter the default Ubuntu Apache web page.
This web page confirms that Apache is functioning correctly. It also provides basic information about important Apache files and directory locations.
Configuring Apache Virtual Hosts
Setting up virtual hosts is necessary to serve multiple domains from a single Apache web server. Here, we'll guide you through establishing a virtual host in Apache using the domain name "testdomain.com." Remember to replace "testdomain.com" with your specific domain name.
STEP 1: Create the directory for your domain:
In this step, we'll establish a directory for our domain name to store our website's data.
Run the following command in the Terminal, replacing "testdomain.com" with your domain name:
sudo mkdir /var/www/your_domain
Next, assign ownership of the directory using the $USER environment variable:
sudo chown -R $USER:$USER /var/www/domain_name
If necessary, ensure that the permissions of your web roots are correct by adjusting your unmask value. However, you can confirm by typing the following:
sudo chmod -R 755 /var/www/domain_name
Then, create a sample index.html page using vi or your preferred text editor:
vi /var/www/your_domain/index.html
Inside, add the following sample HTML:
When you're done, save and close the file. If you used vi, exit by pressing Esc, wq, and then hit ENTER.
Creating a virtual host file with the correct directives is essential for Apache to serve this content. Instead of directly modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf, create a new one at /etc/apache2/sites-available/domain_name.conf:
sudo vi /etc/apache2/sites-available/domain_name.conf
Include the following configuration block, like the default but updated for your new directory & domain name:
Please note that we've updated the DocumentRoot to our new directory and set ServerAdmin to an email accessible by your domain site administrator.
Additionally, two directives have been added: ServerName, which sets the base domain for this virtual host definition, and ServerAlias, which defines additional domains that should match as though they were the base name.
Save & close the file when you're done.
Next, enable the file using the a2ensite tool:
sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
Now, test for configuration errors:
sudo apache2ctl configtest
You should see the following output:
Restart Apache to apply your changes:
sudo systemctl restart apache2
You can verify if Apache serves your domain name correctly by visiting http://domain_name in your web browser. You should see something similar to the following:
Managing Apache Server
You can use several commands in the Terminal to manage the Apache server. Here are some useful ones:
To start the Apache server, use the command:
sudo systemctl start apache2
To stop the Apache server, use the command:
sudo systemctl stop apache2
To reload the Apache server and update the new configurations, use the command:
sudo systemctl reload apache2
To start Apache at boot, use the command:
sudo systemctl enable apache2
To disable Apache at boot, use the command:
sudo systemctl disable apache2
Conclusion
In conclusion, this article offers a thorough guide to configuring the Apache web server on Ubuntu or Debian VPS. Following the steps outlined, users can effectively set up and manage their web server infrastructure to host websites or web applications.
From installation to virtual host configuration, this tutorial covers essential aspects of Apache server management, enabling users to establish a reliable and secure web hosting environment. With these skills, users can confidently deploy and maintain their online projects, ensuring optimal performance and accessibility for their visitors.