Edited 1 day ago by ExtremeHow Editorial Team
ApacheWeb ServerUbuntuLinuxInstallationConfigurationOperating SystemsServerSystemSetup
This content is available in 7 different language
The Apache HTTP Server, commonly known as Apache, is a free and open-source web server software. It is one of the most popular web server systems and is known for its robustness, flexibility, and widespread use. In this guide, we will walk you through the steps to install and configure Apache on Ubuntu systems. The aim of this guide is to provide you with a simple yet comprehensive walkthrough to help you set up your web server quickly and efficiently.
Before we begin, let's look at some requirements that you must have to follow this guide:
It is always a good practice to update your package index before installing any new software on your computer. This ensures that you are downloading the latest versions of the software you want to install. You can update the package index using the following command:
sudo apt update
This command uses the package manager apt
in Ubuntu to update the list of available software packages and their respective versions. It does not upgrade or install any packages at this stage.
After updating the package index, the next step is to install Apache. You can install Apache by executing the following command:
sudo apt install apache2
This command will install Apache on your system. During the installation, you may be asked to confirm that you want to continue. You can confirm this by typing Y
and pressing Enter
.
Once the installation is complete, Apache starts automatically. You can check its status using the following command:
sudo systemctl status apache2
This will return information about the Apache service. Look for the line that says "Active (running)" which indicates that everything is working as expected.
It is important to make sure that your firewall allows HTTP and HTTPS traffic so that web requests can successfully reach your Apache server. Ubuntu systems can use ufw
(Uncomplicated Firewall) to manage firewall rules. You can check if ufw
is active with the command:
sudo ufw status
If it is activated, you must grant permission to Apache by executing one of the following commands:
sudo ufw allow 'Apache'
Or alternatively to specifically allow both HTTP and HTTPS:
sudo ufw allow 'Apache Full'
Remember to reload the firewall using the command:
sudo ufw reload
Then verify the changes:
sudo ufw status
You should see Apache-related rules in the output.
With Apache installed, you can verify that it is working by accessing your server's public IP address in a web browser. If you don't know your server's IP address, you can find it using:
hostname -I
In your web browser, enter your IP address in the address bar (for example, http://your_server_ip
) and press Enter
. You will see the default Apache welcome page, indicating that Apache is installed correctly and is serving web content.
Apache's default configuration works without any problems, but let's take a look at some specific customizations:
The default document root, where Apache looks for files, is /var/www/html
. You can change this if you want by editing the configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Look for DocumentRoot
directive, which is usually at the top of the file. Change its value to the path you want, for example:
DocumentRoot /path/to/your/project
Save the file and exit the text editor. Restart Apache to apply these changes:
sudo systemctl restart apache2
.htaccess
files are directory-level configuration. To allow the use of .htaccess
, locate the corresponding <Directory>
block:
sudo nano /etc/apache2/apache2.conf
Find the following sections:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Modify AllowOverride None
to AllowOverride All
:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save the changes and restart Apache:
sudo systemctl restart apache2
Virtual hosts allow you to host multiple websites on the same server. To create a virtual host, first create a directory for your site:
sudo mkdir -p /var/www/example.com
Assign ownership to the specified directory:
sudo chown -R $USER:$USER /var/www/example.com
Create a new configuration file for the site:
sudo nano /etc/apache2/sites-available/example.com.conf
Add this basic configuration:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable your new virtual host with these commands:
sudo a2ensite example.com.conf
sudo systemctl reload apache2
Make sure to disable the default site:
sudo a2dissite 000-default.conf
Reload Apache to apply all changes:
sudo systemctl reload apache2
After configuring your virtual host or making changes to Apache's configuration files, it is important to test these changes to identify any errors. Use:
sudo apache2ctl configtest
You will see a "Syntax is OK" message. If not, resolve any errors reported.
By following these steps, you have installed and configured Apache on Ubuntu. You have also configured some additional features such as a custom document root, enabled .htaccess
support, and created a virtual host. Apache provides extensive capabilities beyond the basics covered here, including modules, security settings, and performance optimization. You can explore these as you gain more experience and knowledge in web server administration.
As you continue to develop and deploy applications, remember to regularly update your Ubuntu packages and Apache version to include the latest features and security improvements.
If you find anything wrong with the article content, you can