How to Install Apache Web Server on Ubuntu 18.04
The Apache HTTP server is a free, open-source, powerful, stable, reliable, and the most widely-used cross-platform web server available. It runs on Unix-like systems, such as Linux and Windows operating systems. It offers many strong features containing dynamically loadable modules, powerful media support, and immense integration with other popular software. It also works as a reverse proxy for other servers, for example, application servers such as Nodejs, Python, and more.
In this tutorial, we will show you how to install an Apache web server for Ubuntu 18.04. We are going to look at how you can manage the Apache service via your system and create virtual hosts for setting up websites.
Step 1: Installing Apache on Ubuntu 18.04
Apache is available from the official Ubuntu software repositories; start by updating your system’s package index and then install the Apache package as well as its dependencies using the APT package manager:
$ sudo apt update $ sudo apt install apache2
After you have successfully installed the Apache web server, take note of the Apache important default files and directories.
- Main config files directory: /etc/apache2/.
- Main config file: /etc/apache2/apache2.conf.
- Additional configuration snippets: /etc/apache2/conf-available/and /etc/apache2/conf-enabled/.
- Per-site virtual hosts configuration snippets: /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/.
- Configuration snippets for loading modules: /etc/apache2/mods-available/ and /etc/apache2/mods-enabled/.
- Web Document Root: /var/www/html/.
- Log files (error and access logs) directory: /var/log/apache/.
Once the Apache installation process is done, the web server service will start automatically. You can also check if it is up and running by using the command below:
$ sudo systemctl status apache2
Now that your Apache web server is up and running, we can go over some basic management commands to manage the Apache service using those outlined below:
$ sudo systemctl status apache2 $ sudo systemctl stop apache2 $ sudo systemctl start apache2 $ sudo systemctl restart apache2 $ sudo systemctl reload apache2 $ sudo systemctl enable apache2 $ sudo systemctl disable apache2
Now, if you have the UFW firewall enabled and running on your system, you have to open ports 80 and 443 to allow client requests to the Apache web server via HTTP and HTTPS respectively.
Then, reload the firewall settings with the commands below:
$ sudo ufw allow 80/tcp $ sudo ufw allow 443/tcp $ sudo ufw reload
Step 2: Checking Apache Web Server on Ubuntu 18.04
Now try to see if your Apache2 installation is working well; open a web browser and enter the URL below to access the Apache default web page.
You should see a web page which says it works; it should also show some basic information about important Apache configuration files and directory locations.
Remember that if you want to use a dummy domain name like dreamvps.local, which is not fully registered, you will need to set up a local DNS with the /etc/hosts file on the machine where you will be accessing the Apache default web page.
$ sudo vim /etc/hosts
Now append the following line to the bottom of the file; be sure to replace 192.168.56.101 and dreamvps.local with your server’s IP address and local domain name.
Step 3: Setting Up Apache Virtual Hosts on Ubuntu 18.04
Now we are going to show you how you can create a virtual host with Apache HTTP server (similar to Nginx server blocks) for your websites. For example, if you have a site named example.com that you would like to host on your VPS using apache, you have to create a virtual host for it under /etc/apache2/sites-available/
Begin by first creating your document root directory for your domain, example.com, where your site’s files are going to be stored.
$ sudo mkdir -p /var/www/html/example.com/
Now set the appropriate permissions on the directory as shown below:
$ sudo chmod -R 775 /var/www/html/example.com/ $ sudo chown -R www-data:www-data /var/www/html/example.com/
Now create a test html index page for your site in the root directory of your website.
$ sudo vim /var/www/html/example.com/index.html
Inside, append the following sample HTML code.
<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>The example.com virtual host is working!</h1> </body> </html>
Save and close the file after you are finished.
Now enable your site configuration using the a2ensite utility.
$ sudo a2ensite example.com.conf
Next, test your Apache2 config for any errors, if all is okay, restart the apache2 service as shown.
$ sudo apache2ctl configtest $ sudo systemctl restart apache2
Since the domain name example.com is a dummy domain (not a fully registered domain), you also need to set up a local DNS by adding it to the /etc/hosts file.
$ sudo vim /etc/hosts
Then append the following line at the bottom of the file, making sure to replace 192.168.56.101 and example.com with your server’s IP address and local domain name.
Save the file and exit.
Finally, open a web browser and access the test site’s index pages using the following URLs.
Test VirtualHost Website
That’s it! In this article, we have explained how to install an Apache web server on Ubuntu 18.04. We also looked at how to manage the Apache2 process via systemd and create and enable per-site virtual host configurations. If you have any queries, use the comment form below to contact us.