Apache is one of the most popular web servers used to host websites and applications. In this guide, we’ll walk you through installing and configuring Apache on an Ubuntu system.
Step 1: Update Package Index
Before installing Apache, update the package index to ensure you have the latest software:
<code>sudo apt update && sudo apt upgrade -y</code>
Step 2: Install Apache
Use the following command to install Apache:
<code>sudo apt install apache2 -y</code>
Once the installation is complete, Apache will start automatically.
Step 3: Enable and Start Apache Service
To ensure Apache runs at startup, enable it using:
<code>sudo systemctl enable apache2 sudo systemctl start apache2</code>
To check the status of Apache:
<code>sudo systemctl status apache2</code>
Step 4: Configure the Firewall
Allow HTTP and HTTPS traffic through the firewall:
<code>sudo ufw allow 'Apache Full'</code>
Enable the firewall if it's not already enabled:
<code>sudo ufw enable</code>
Verify firewall status:
<code>sudo ufw status</code>
Step 5: Verify Apache Installation
To check if Apache is running, open your web browser and go to:
<code>http://your-server-ip</code>
You should see the Apache default welcome page.
Step 6: Manage Apache Service
Here are some useful Apache service commands:
Restart Apache:
<code>sudo systemctl restart apache2</code>
Stop Apache:
<code>sudo systemctl stop apache2</code>
Reload Apache without restarting:
<code>sudo systemctl reload apache2</code>
Step 7: Configure Virtual Hosts (Optional)
If you want to host multiple websites, you need to configure virtual hosts. Here’s an example:
Create a new directory for your website:
<code>sudo mkdir -p /var/www/example.com/html sudo chown -R $USER:$USER /var/www/example.com/html sudo chmod -R 755 /var/www/example.com</code>
Create an HTML test page:
<code>echo '<h1>Welcome to Example.com</h1>' | sudo tee /var/www/example.com/html/index.html</code>
Create a new virtual host configuration file:
<code>sudo nano /etc/apache2/sites-available/example.com.conf</code>
Add the following content:
<code><VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost></code>Save and exit (CTRL+X, then Y, then ENTER).
Enable the new virtual host and restart Apache:
<code>sudo a2ensite example.com.conf sudo systemctl restart apache2</code>
Conclusion
You’ve successfully installed and configured Apache on Ubuntu! You can now host websites and applications on your server. If you need SSL/TLS, consider setting up Let’s Encrypt with Certbot.