The Apache HTTP Server, commonly known as Apache, is one of the most popular and widely used web servers. This guide will walk you through the steps required to install, configure, and manage Apache on an Ubuntu system.
Prerequisites #
Before you begin, make sure you have the following:
- Ubuntu Server or Desktop: A system running Ubuntu (18.04/20.04/22.04).
- Root or Sudo Privileges: You need administrative access to install and configure Apache.
- Basic Terminal Skills: Familiarity with Linux command-line utilities.
Step-by-step #
- Follow these steps
Step 1: SSH into your server as root. See these steps on how to. #
Step 2: Update the Package Index #
- It’s always good practice to update your package list before installing any new software.
sudo apt update
Step 3: Install Apache #
- To install Apache, run the following command:
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.
- Once done, you can check apache version using the command below;
apache2 -version
Step 4: Adjust the Firewall to Allow Apache #
- If you have a firewall enabled on your server, you’ll need to open ports for Apache. Apache typically uses port
80
for HTTP and port443
for HTTPS. - To allow traffic on these ports, use the following command:
sudo ufw allow 'Apache'
- To verify that the rules were added successfully, run:
sudo ufw status
Step 5: Check Apache Status #
- After installation, Apache should start automatically. You can check its status using the following command:
sudo systemctl status apache2
You should see a message indicating that the service is active and running.
Step 6: Test Apache Installation #
- To test that Apache is working correctly, open a web browser and enter your server’s IP address:
- You can get your IP bu running hostname -I on your terminal, then access the it on your browser.
http://your-server-ip
- You should see the default Apache welcome page, confirming that the web server is running correctly.
Step 7: Managing Apache Service #
- You can manage the Apache service using the
systemctl
command. Here are some common tasks:- Start Apache: sudo systemctl start apache2
- Stop Apache: sudo systemctl stop apache2
- Restart Apache: sudo systemctl restart apache2
- Reload Apache (without stopping): sudo systemctl reload apache2
- Enable Apache to Start at Boot: sudo systemctl enable apache2
Step 8: Configure Apache Virtual Hosts #
Apache uses virtual hosts to serve multiple domains from the same server. Here’s how you can create a virtual host configuration.
- Create the Directory for the Website:
sudo mkdir -p /var/www/your_domain/public_html/
- Assign Ownership: Assign ownership of the directory to the user running the web server (typically
www-data
):
sudo chown -R www-data:www-data /var/www/your_domain/public_html
- Create a Virtual Host Configuration File: Apache stores virtual host configurations in
/etc/apache2/sites-available/
. Create a new configuration file for your domain:
sudo vim /etc/apache2/sites-available/your_domain.conf
- Add the following content: Replace your_domain with your domain name
<VirtualHost *:80>
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/your_domain.com/public_html
ErrorLog /var/www/your_domain.com/error.log
CustomLog /var/www/your_domain.com/requests.log combined
</VirtualHost>
- Enable the Virtual Host: Enable your virtual host by running:
sudo a2ensite your_domain.conf
- Disable the Default Virtual Host (Optional): If you no longer want to serve the default Apache page:
sudo a2dissite 000-default.conf
- Reload Apache: For the changes to take effect:
sudo systemctl reload apache2
Step 8: Test if your set up can serve a website
- Create a sample index.html file in your document root, using vim or nano or your preferred text editor:
vi /var/www/your_domain/public_html/index.html
- Add the following sample HTML code inside the file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Text</title>
<style>
.styled-text {
font-family: Arial, sans-serif;
font-size: 20px;
color: #333;
line-height: 1.5;
text-align: center;
margin-top: 50px;
}
</style>
</head>
<body>
<div class="styled-text">
Yoh! You get me?.<br>
My apache set up is working. Let's celebrate!
</div>
</body>
</html>
- Save the file once you have added the code and restart apache.
sudo systemctl restart apache2
- Visit your domain on the browser to see if it displays content of your index.html file.