How to Install and Configure the Apache Web Server on Ubuntu 16.04/17.04/18.04
This post will guide you how to install and configure the Apache Web Server on your Ubuntu Linux 18.04 server. How do I configure the Apache Http server in Ubuntu Linux step by step.
- What is Apache Http Server
- Install apache Http server
- Configure Apache Http Server
- Checking Apache Http Server
- Apache Http Server Configuration Files
- Creating Your Own Website (Setting Up Virtual Hosts)
What is Apache Http Server?
The Apache HTTP Server is an open-source HTTP server for modern operating systems including UNIX, Microsoft Windows, Mac OS/X and Netware. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services observing the current HTTP standards. Apache has been the most popular web server on the Internet since April of 1996.
Install Apache Http Server
Before installing the Apache Http server, you need to make sure that you are logged in as a user with sudo privileges so that you can install packages with default ubuntu source list.
You do not need to add extract software repositories into your ubuntu 18.04 Linux. As the Apache installation package can be found in the default ubuntu repositories. And you just need to use apt or apt-get command to install the apache2 package. You need to update the package index firstly, and then install the newly package. Type the following command:
$ sudo apt-get update $ sudo apt-get install apache2
Outputs:
devops@devops-osetc:~$ sudo apt-get install apache [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done Package apache is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'apache' has no installation candidate devops@devops-osetc:~$ sudo apt-get install apache2 Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: amd64-microcode btrfs-tools gcc-6-base:i386 intel-microcode iucode-tool libboost-random1.58.0 libboost-thread1.58.0 libcapnp-0.5.3 libevent-core-2.0-5 libhardware2 libhybris libhybris-common1 libiscsi2 libmedia1 librpm3 librpmbuild3 librpmio3 librpmsign3 libwebpdemux1 libwxgtk-media3.0-0v5 libwxgtk-media3.0-dev libwxgtk3.0-0v5 libwxgtk3.0-dev libxen-4.6 linux-generic linux-generic-hwe-16.04 linux-headers-generic linux-headers-generic-hwe-16.04 linux-image-generic linux-image-generic-hwe-16.04 qml-module-qtwebkit snap-confine snapd-login-service thermald xserver-xorg-input-evdev-hwe-16.04 xserver-xorg-input-synaptics-hwe-16.04 xserver-xorg-video-amdgpu-hwe-16.04 xserver-xorg-video-qxl-hwe-16.04 xserver-xorg-video-vesa-hwe-16.04 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: apache2-data apache2-utils Suggested packages: apache2-doc apache2-suexec-pristine | apache2-suexec-custom The following NEW packages will be installed: apache2 apache2-data apache2-utils 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 339 kB of archives. After this operation, 1,757 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 apache2-utils amd64 2.4.29-1ubuntu4.5 [83.3 kB] Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 apache2-data all 2.4.29-1ubuntu4.5 [160 kB] Get:3 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.5 [95.1 kB] Fetched 339 kB in 6s (53.7 kB/s) Selecting previously unselected package apache2-utils. (Reading database ... 199040 files and directories currently installed.) Preparing to unpack .../apache2-utils_2.4.29-1ubuntu4.5_amd64.deb ... Unpacking apache2-utils (2.4.29-1ubuntu4.5) ... Selecting previously unselected package apache2-data. Preparing to unpack .../apache2-data_2.4.29-1ubuntu4.5_all.deb ... Unpacking apache2-data (2.4.29-1ubuntu4.5) ... Selecting previously unselected package apache2. Preparing to unpack .../apache2_2.4.29-1ubuntu4.5_amd64.deb ... Unpacking apache2 (2.4.29-1ubuntu4.5) ... Processing triggers for ufw (0.35-5) ... Processing triggers for ureadahead (0.100.0-20) ... Setting up apache2-utils (2.4.29-1ubuntu4.5) ... Setting up apache2-data (2.4.29-1ubuntu4.5) ... Processing triggers for systemd (237-3ubuntu10.11) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Setting up apache2 (2.4.29-1ubuntu4.5) ... Enabling module mpm_event. Enabling module authz_core. Enabling module authz_host. Enabling module authn_core. Enabling module auth_basic. Enabling module access_compat. Enabling module authn_file. Enabling module authz_user. Enabling module alias. Enabling module dir. Enabling module autoindex. Enabling module env. Enabling module mime. Enabling module negotiation. Enabling module setenvif. Enabling module filter. Enabling module deflate. Enabling module status. Enabling module reqtimeout. Enabling conf charset. Enabling conf localized-error-pages. Enabling conf other-vhosts-access-log. Enabling conf security. Enabling conf serve-cgi-bin. Enabling site 000-default. Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service. Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service. insserv: warning: current start runlevel(s) (empty) of script `apache-htcacheclean' overrides LSB defaults (2 3 4 5). insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `apache-htcacheclean' overrides LSB defaults (0 1 6). Processing triggers for ureadahead (0.100.0-20) ... Processing triggers for systemd (237-3ubuntu10.11) ... Processing triggers for ufw (0.35-5) ...
You need to enter Y to contiute the installation process. Then the Apache package is installed on your Ubuntu 18.04 Linux.
Configure Apache Http Server
After installed the Apache packages, you need to adjust the firewall policy to enable to two prots: http (80) and https (443) so that you can access your web server via http protocal or web browser.
As the default firewall management tool is the UFW on Ubuntu Linux. So if you want to enable the most restrictive profile that will still allow the traffic you have configured, permitting traffic on port 80 or 8080 or 443. You can execute the following command to adjust your filewall:
$ sudo ufw allow 'Apache'
Or
$ sudo ufw allow 'Apache Full'
Outputs:
devops@devops-osetc:~$ sudo ufw allow 'Apache Full'
Rules updated
Rules updated (v6)
Before running the above command, you can also check the available ufw application profiles in your current Ubuntu Linux Server. Type:
$ sudo ufw app list
Outputs:
devops@devops-osetc:~$ sudo ufw app list Available applications: Apache Apache Full Apache Secure CUPS OpenSSH
From the above outputs, you can see that there are some Apache profiles that can be used to update its rules.
You can also use the following command to check the rule change about the Apache Full profile. Type the following command:
$ sudo ufw status
Outputs:
devops@devops-osetc:~$ sudo ufw status
Status: active
To Action From
-- ------ ----
Apache Full ALLOW Anywhere
OpenSSH ALLOW Anywhere
Apache Full (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
Checking Apache Http Server
After installed the Apache Http Server in the first step, you need to check the Apache service if it is running with the systemd init system. Type the following command:
$ sudo systemctl status apache2.service
Outputs:
devops@devops-osetc:~$ sudo systemctl status apache2.service ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Wed 2018-12-26 04:04:06 EST; 19min ago Main PID: 16145 (apache2) Tasks: 55 (limit: 1107) CGroup: /system.slice/apache2.service ├─16145 /usr/sbin/apache2 -k start ├─16147 /usr/sbin/apache2 -k start └─16148 /usr/sbin/apache2 -k start Dec 26 04:04:05 devops-osetc systemd[1]: Starting The Apache HTTP Server... Dec 26 04:04:06 devops-osetc apachectl[16134]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 2606:b400:c0 Dec 26 04:04:06 devops-osetc systemd[1]: Started The Apache HTTP Server.
If you can see that the Active status is active (running), it indicates that the Apache HTTP Server is started successfull on your Ubuntu 18.04 Linux Server.
Then you can verify your Apache Web Server if it works well. And you need to open your web browser to access the default Apache landing page to confirm that the installed Apache software is running properly. Type your server ip address with http protocol in web browser:
http://server_ip
You should see the default Ubuntu Apache web page, Like as shown screenshot:
If the Apache service is not running, you should start it with the following command:
$ sudo systemctl start apache2.service
If you want to stop the Apache service, Type the following command:
$ sudo systemctl stop apache2.service
If you want to restart the Apache service, type the following command:
$ sudo systemctl restart apache2.service
If you want to reload the Apache service, type:
$ sudo systemctl reload apache2.service
If you want to start the Apache service automatically at system boot, type:
$ sudo systemctl enable apache2.service
Apache Http Server Configuration Files
/etc/apache2: All Apache configuration files are stored in the this directory.
/etc/apache2/apache2.conf: it is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.
/etc/apache2/ports.conf: it is included from the main configuration file. It is used to determine the listening ports for incoming connections and this file can be customized anytime.
/etc/apache2/sites-available/: All Virtual Hosts configuration files are stored in this directory.
/etc/apache2/mods-available/: contains configuration files to both load modules and configure them.
Creating Your Own Website (Setting Up Virtual Hosts)
When you installed the Apache Packages, it has created a basic site called “Apache2 Ubuntu Default page” (we saw it in the previous step). You can modify its content in the /var/www/html directory or settings by editing its Virtual Host file located in /etc/apache2/sites-enabled/000-default.conf.
If you want to create your own website called test.com on your Apache web server, you also need to create one directory under /var/www/ and create one new site configuration file under /etc/apache2/sites-available/test.com.conf. Just do the following steps:
#1 create the directory for your own site test.com with mkdir command under /var/www directory. Type:
$ sudo mkdir -p /var/www/test.com/html
Note: you need to use -p option for mkdir command so that to create any necessary parent directories.
#2 Change Ownership for you newly created directory, type:
$ sudo chown -R $USER:$USER /var/www/test.com/html
#3 Change the permission of your web root directory, type:
$ sudo chmod -R 755 /var/www/test.com
#4 create a sample index.html or index.php file via vim text editor, type:
$ sudo vim /var/www/test.com/html/index.html
And then adding the following lines:
<html> <head> <title>Welcome to test.com site!/title> </head> <body> <h1>this is test.com </h1> </body> </html>
Save and close the file.
#5 create a new virtual host configuration file called test.com.conf under /etc/apache2/sites-available/ directory. Then adding the following lines:
<VirtualHost *:80> ServerAdmin admin@test.com ServerName test.com ServerAlias www.test.com DocumentRoot /var/www/test.com/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save and close the file.
#6 After setting up your test.com website, you need to activate the virtual hosts configuration file to enable it. And you can run the following commands:
$ sudo cd /etc/apache2/sites-available/ $ sudo a2ensite test.com.conf
Outputs:
Enabling site test.com. To activate the new configuration, you need to run: systemctl reload apache2
#7 you can disable the default virtual host defined in /000-default.conf, type:
$ sudo a2dissite 000-default.conf
#8 Test for configuration errors, type:
$ sudo apache2ctl configtest
Outputs:
Syntax Ok
#9 You need to restart or reload the Apache service. Type:
$ sudo systemctl reload apache2
Or
$ sudo systemctl restart apache2
#10 you can type your site domain name in a web browser to check if you can saw your first html page. Type:
http://test.com
Done!