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 dnf update -y
Step 3 : Install Apache #
- To install Apache, run the following command:
sudo dnf install httpd -y
Step 3: Start and Enable Apache #
After installing Apache, you need to start the Apache service and enable it to start automatically on boot.
- Start Apache:
sudo systemctl start httpd
- Enable Apache on Boot:
sudo systemctl enable httpd
- Check Apache Status:
sudo systemctl status httpd
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 commands:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
- To verify that the rules were added successfully, run to relaod the firewall:
sudo firewall-cmd --reload
Step 5: Check Apache Status #
- After installation, Apache should start automatically. You can check its status using the following command:
sudo systemctl status httpd
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 httpd
- Stop Apache: sudo systemctl stop httpd
- Restart Apache: sudo systemctl restart httpd
- Reload Apache (without stopping): sudo systemctl reload httpd
- Enable Apache to Start at Boot: sudo systemctl enable httpd
Step 7: 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
- 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
- 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/httpd/sites-available/your_domain.conf
- Add the following content: Replace admin@your_domain with your valid email and your_domain with your domain name
<VirtualHost *:80>
ServerAdmin admin@your_domain
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<VirtualHost *:80>
- 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 restart httpd
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/index.html
- Add the following sample HTML code inside the file:
<html>
<head>
<h2>This is a test page</>
</head>
<body>
<p>My set up is working</p>
</body>
</html>
- Save the file once you have added the code and restart apache.
sudo systemctl restart httpd
- Visit your domain on the browser to see if it displays content of your index.html file.
Step 9: Enable PHP Support (Optional) #
- If you plan to use PHP with Apache, you’ll need to install PHP and its modules.
Install PHP:
sudo dnf install php php-mysqlnd php-fpm -y
Restart Apache:
sudo systemctl restart httpd
Test PHP:
sudo nano /var/www/html/example.com/test.php
Add the following content:
<?php
phpinfo();
?>
- Save and exit. Then, navigate to
http://your-server-ip/test.php
in your browser. If PHP is configured correctly, you should see the PHP info page.