Apache or Apache HTTP server is a free and open source web server, developed and maintained by the Apache Software Foundation. Its popularity can be judged by the fact that around 46% of the websites worldwide are powered by Apache. Apache allows website developers to serve their content over the web. It serves as a delivery man by delivering files requested by users when they enter a domain name in their browser’s address bar.
This tutorial is about installing and configuring Apache2 on your Ubuntu system. The commands and procedures mentioned in this article have been run on an Ubuntu 18.04 LTS system. Since we are using the Ubuntu command line, the Terminal, in this article; you can open it through the system Dash or the Ctrl+Alt+T shortcut.
Please follow the following steps in order to install the Apache2 software through Ubuntu official repositories.
You can download the latest version of a software by first updating the local package index of Ubuntu repositories. Open the Terminal and enter the following command in order to do so:
$ sudo apt update
Next, enter the following command as sudo in order to install Apache2 and its required dependencies:
$ sudo apt install apache2
You may be prompted with a y/n option to continue installation. Please enter Y, after which the installation procedure will begin.
When the installation is complete, you can check the version number and thus verify that Apache2 is indeed installed on your system by entering the following command:
$ apache2 -version
In order to configure Apache, we first need to allow outside access to certain web ports of our system and allow Apache on your UFW firewall.
In order to configure the firewall, let us first list the application profiles we will need to enable access to Apache. Use the following command to list such available applications:
$ sudo ufw app list
In the above output, you can see three Apache profiles all providing different levels of security; Apache being the one that provides maximum restriction with port 80 still open.
Allowing Apache on UFW will open port 80 for network traffic, while providing maximum security to the server. Please configure UFW to allow Apache through the following command:
$ sudo ufw allow 'Apache'
The status of UFW will now display Apache enabled on the firewall.
$ sudo ufw status
The first step is to verify that the Apache2 service is up and running on your system, through the following command:
$ sudo systemctl status apache2
The status “active (running) verifies that the apache2 service is running.
You can also verify if Apache is running by requesting a page from the Apache server. For this purpose, you can use your server’s IP in order to access the Apache landing page.
Use the following command to know about your server’s IP:
$ hostname -I
Then try the IPs, one by one from the output, in your web browser as follows:
http://server_IP
In my case, http://192.168.100.4 and http://192.168.100.5. Doing so will display the following Apache web page for Ubuntu, verifying that the Apache server is working properly.
A virtual host is similar to what you have server blocks in Nginx. It is used to manage configurations for more than one domain from one server. We will present an example of how to set up a virtual host through the Apache server. We will set up a website named sampledomain.com by using the server block that is enabled by default in Apache for Ubuntu 18.
The server block that is enabled by default is capable of serving documents from /var/www/html. However, we will create a directory at /var/www/ leaving the default directory intact.
Create this directory through the following command, replacing sampledomain.com by your respective domain name.
sudo mkdir -p /var/www/sampledomain.com/html
Then assign the ownership of the directory through the following commands:
sudo chown -R $USER:$USER /var/www/sampledomain.com/html
sudo chmod -R 755 /var/www/sampledomain.com
Let us now create an index page that we can later access to test if Apache is running our domain name. Create an HTML file either through the Nano editor or any of your favorite text editor.
$ nano /var/www/sampledomain.com/html/index.html
Enter the following HTML for the index page:
<html> <head> <title>Welcome to the page sampledomain.com!</title> </head> <body> <h1>You got Lucky! Your sampledomain.com server block is up!</h1> </body> </html>
You can save a file in nano by using Ctrl+X and then enter Y and hitting Enter.
Apache needs a virtual host file to serve the contents of your server. The default configuration file for this purpose is already created but we will make a new one for our custom configurations.
$ sudo nano /etc/apache2/sites-available/sampledomain.com.conf
Enter the following customized configuration details for our domain name:
<VirtualHost *:80>
ServerAdmin admin@sampledomain.com
ServerName sampledomain.com
ServerAlias www.sampledomain.com
DocumentRoot /var/www/sampledomain.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> You can save a file in nano by using Ctrl+X and then enter Y and hitting Enter.
Let us enable the configuration file we created with the a2ensite tool:
$ sudo a2ensite sampledomain.com.conf
The output will suggest activating the new configuration but we can do it all collectively after running the following command that disables the original configuration file:
$ sudo a2dissite 000-default.conf
Now restart the Apache service:
$ sudo systemctl restart apache2
Finally, let us test if there are any configuration errors through the following command:
$ sudo apache2ctl configtest
If you do not get any errors, you will get the following output:
However, the following error is common in Ubuntu 18.04
Resolve the error:
Enter the following command in order to resolve the above-mentioned error:
$ echo "ServerName sampledomain.com | sudo tee /etc/apache2/conf-available/servername.conf
And then:
$ sudo a2enconf servername
Now when you check again for errors, you will see this error resolved through the following output:
Apache server is now configured to serve your domain name. This can be verified by entering your server name as follows in any of the web browsers running on your system:
http://sampledomain.com
The index page should display as follows, indicating that Apache is now ready to serve your server block!
After setting up the web server, you might have to perform some basic management operations on Apache. Here are the commands that you can enter in your Terminal application for these operations.
sudo systemctl start apache2
Use this command as sudo in order to start the Apache server.
sudo systemctl stop apache2
Use this command as sudo in order to stop the Apache server when it is in start mode.
sudo systemctl restart apache2
Use this command as sudo in order to stop and then start the Apache service again.
sudo systemctl reload apache2
Use this command as sudo in order to apply the configuration changes without restarting the connection.
sudo systemctl enable apache2
Use this command as sudo in order to enable Apache to be started every time you boot your system.
sudo systemctl disable apache2
Use this command as sudo in order to disable if you have set up Apache to be started every time you boot your system.
Through this article, you have learned to install and configure the Apache web server on your Ubuntu system. This includes making some changes to your UFW firewall and then configuring your web server for your IP address. We also recommend you to set up a virtual host through Apache; this will give you a basis on how to use Apache to host your files on the Internet. The basic Apache management commands will also help you as a web administrator to manage your web server in an optimal manner.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…