Tutorial How To Install The Apache Web Server On Ubuntu 20.04. The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software. Do not waste the time and buy your own Ubuntu VPS to go through a real installation.
To let this tutorial work better, please consider the below Prerequisites:
- A non-root user with sudo privileges
- To set up, follow our Initial server setup on Ubuntu 20.04
Table of Contents
How To Install The Apache Web Server On Ubuntu 20.04
It will be easy to learn How to install the Apache Web Server on Ubuntu 20.04 if you follow the steps of this guide correctly.
1- How To Install Apache
While Apache is available within Linux Ubuntu’s default software repositories you can begin by updating the local package index to reflect the latest upstream changes. Making it possible to install it using conventional package management tools.
sudo apt update
And then to install the apache2 package:
sudo apt install apache2
2- How To Adjust The Firewall
In this step, before testing Apache, it’s necessary to modify the firewall settings to allow outside access to the default web ports. Assuming that you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.
Notice that during installation, Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.
To list the ufw application profiles run the command below:
sudo ufw app list
Available applications: Apache Apache Full Apache Secure OpenSSH
As it is clear by output, there are three profiles available for Apache:
- Apache: This profile opens only port 80 (normal, unencrypted web traffic)
- Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
Note: Also it is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:
sudo ufw allow 'Apache'
Next, use the following command to verify the change.
sudo ufw status
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
3- How To Check Your Web Server
Ubuntu 20.04 starts Apache when the installation process is ended. The web server should already be up and running.
You can make sure the service is running by typing:
sudo systemctl status apache2
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-04-23 22:36:30 UTC; 20h ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 29435 (apache2) Tasks: 55 (limit: 1137) Memory: 8.0M CGroup: /system.slice/apache2.service ├─29435 /usr/sbin/apache2 -k start ├─29437 /usr/sbin/apache2 -k start └─29438 /usr/sbin/apache2 -k start
Due to this output confirmation, the service has started successfully. However, the best way to test this is to request a page from Apache.
You can access the default Apache landing page to confirm that the software is running properly through your IP address. If you do not know your server’s IP address, you can get it a few different ways from the command line.
Type this at your server’s command prompt:
hostname -I
You will get back a few addresses separated by spaces. You can try each in your web browser to determine if they work.
Another option is to use the Icanhazip tool, which should give you your public IP address as read from another location on the internet:
url -4 icanhazip.com
Enter your server’s IP address into your browser’s address bar:
http://your_server_ip
Then you will see the default Ubuntu 20.04 Apache web page:
Once you can see this page, it means that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.
4- How To Manage The Apache Process
You are ready to go over some basic management commands because the webserver is up and running.
To stop your web server:
sudo systemctl stop apache2
By running the command below, start the webserver when it is stopped.
sudo systemctl start apache2
Use the following command to stop and then start the service again.
sudo systemctl restart apache2
Apache can often reload without dropping connections if you are simply making configuration changes.
To do this, use this command:
sudo systemctl reload apache2
Apache is configured to start automatically when the server boots by default. If this is not what you want, disable this behavior by typing:
sudo systemctl disable apache2
Type the command below to re-enable the service to start up at boot.
sudo systemctl enable apache2
5- How To Set Up Virtual Hosts (Recommended)
When using the Apache web server, you can use virtual hosts to encapsulate configuration details and host more than one domain from a single server. You will set up a domain called your_domain, but you should replace this with your own domain name.
Apache on Ubuntu 20.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for a your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.
Create the directory for your_domain:
sudo mkdir /var/www/your_domain
Now, assign ownership of the directory with the $USER environment variable:
sudo chown -R $USER:$USER /var/www/your_domain
The permissions of your web roots should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
sudo chmod -R 755 /var/www/your_domain
Then, create a sample index.html page using nano or your favorite editor:
sudo nano /var/www/your_domain/index.html
Inside, add the following sample HTML:
<html> <head> <title>Welcome to Your_domain!</title> </head> <body> <h1>Success! The your_domain virtual host is working!</h1> </body> </html>
You can save and close the file now.
In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let’s make a new one at /etc/apache2/sites-available/your_domain.conf:
sudo nano /etc/apache2/sites-available/your_domain.conf
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
<VirtualHost *:80> ServerAdmin webmaster@localhost 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>
Note: You have updated the DocumentRoot to our new directory and ServerAdmin to an email that the your_domain site administrator can access. We’ve also added two directives: ServerName, which establishes the base domain that should match for this virtual host definition, and ServerAlias, which defines further names that should match as if they were the base name.
You can save and close the file now.
Use the following command to enable the file with the a2ensite tool:
sudo a2ensite your_domain.conf
To disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
And to test for configuration errors:
sudo apache2ctl configtest
Syntax OK
Next, restart Apache to implement your changes:
sudo systemctl restart apache2
Till here the Apache should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:
6- How To Get Familiar With Important Apache Files And Directories
At this point, you know how to manage the Apache service itself, so you should take a few minutes to familiarize yourself with a few important directories and files.
Content
- /var/www/html: The actual web content, which by default only consists of the default Apache page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Apache configuration files.
Server Configuration
- /etc/apache2: The Apache configuration directory. All of the Apache configuration files reside here.
- /etc/apache2/apache2.conf: The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files in the configuration directory.
- /etc/apache2/ports.conf: This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 and additionally listens on port 443 when a module providing SSL capabilities is enabled.
- /etc/apache2/sites-available/: The directory where per-site virtual hosts can be stored. Apache will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory and then enabled by linking to the other directory with the a2ensite command.
- /etc/apache2/sites-enabled/: The directory where enabled per-site virtual hosts are stored. Typically, these are created by linking to configuration files found in the sites-available directory with the a2ensite. Apache reads the configuration files and links found in this directory when it starts or reloads to compile a complete configuration.
- /etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These directories have the same relationship as the sites-available and sites-enabled directories, but are used to store configuration fragments that do not belong in a virtual host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.
- /etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain the available and enabled modules, respectively. Files ending in .load contain fragments to load specific modules, while files ending in .conf contain the configuration for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod command
Server Logs
- /var/log/apache2/access.log: By default, every request to your web server is recorded in this log file unless Apache is configured to do otherwise.
- /var/log/apache2/error.log: By default, all errors are recorded in this file. The LogLevel directive in the Apache configuration specifies how much detail the error logs will contain.
Conclusion
In this article, you learned How To Install The Apache Web Server On Ubuntu 20.04. Now that you have your web server installed, you have many options for the type of content you can serve and the technologies you can use to create a richer experience. In case you are interested in this subject, find more articles on How to install Apache Web Server on Debian 10 AND How to install WordPress with Apache in Ubuntu 20.04.
Why can’t I host my own web server?
I mention some of the important steps you should consider to be able to do this. Check all and if you did not succeed, send me a message again please.
-Register a domain name.
-Code your website.
-Find out what your IP address is.
-Point your domain name to the IP address of your computer.
-Find out if your ISP supports hosting.
-Ensure your computer at home can support hosting.
-Ensure your computer is secured
I can not configure the firewall
You should try to list the application profiles that you need to give Apache access to. Use the command below to do this.
sudo ufw app list
How to store my data on the server?
To do this, you need to create a directory for your domain name. Remember to replace the info.net with your own domain name and run the following command create a directory for your domain.
sudo mkdir -p /var/www/info.net/html
How to ensure no error exists when the configurations are completed.
Easily! You can test for any configuration errors:
sudo apache2ctl configtest
thank u. I want to disable apache at boot please.
You can run the command below to disable Apache at boot:
sudo systemctl disable apache2