How to Install Apache Web Server on Ubuntu 18.04 LTS
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
The Apache HTTP Web Sever (Apache) is an open source web application for deploying web servers. This guide explains how to install and configure an Apache web server on Ubuntu 18.04 LTS.
If instead you would like to install a full LAMP (Linux, Apache, MySQL and PHP) stack, please see the How to Install a LAMP Stack on Ubuntu 18.04 guide.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Before You Begin
Set up your Linode in the Creating a Compute Instance and Setting Up and Securing a Compute Instance guide.
If you want a custom domain name for your site, you can set this up using our DNS Manager guide.
- Don’t forget to update your
/etc/hosts
file with the public IP address and your site’s fully qualified domain name as explained in the Update Your System’s hosts File section of the Setting Up and Securing a Compute Instance guide.
Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo
. If you’re not familiar with thesudo
command, visit our Users and Groups guide.All configuration files should be edited with elevated privileges. Remember to include
sudo
before running your text editor.- Don’t forget to update your
Install Apache
Install Apache 2.4:
sudo apt-get install apache2
Multi-Processing Modules
Apache 2.4 offers several multi-processing modules (MPMs) to handle connections. In Ubuntu 18.04 LTS the default MPM is the event module, although the prefork module is still recommended if you’re using standard PHP. Below are the basic default settings. For detailed explanations and advanced settings for these modules, see the Tuning Your Apache Server guide.
You can check which MPM is currently configured with the following command:
sudo apachectl -V | grep -i mpm
Server MPM: event
The Prefork Module
The Prefork Module is ideal for single threaded applications. It’s a single parent with multiple forked child servers that are identical processes which wait for incoming requests. Each child process handles a single request. The Prefork Module is resource intensive but necessary for applications that do not support multi-threading such as PHP.
Open
/etc/apache2/mods-available/mpm_prefork.conf
in your text editor and edit the values as needed. The following are the default values:- File: /etc/apache2/mods-available/mpm_prefork.conf
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
On Ubuntu 18.04, the event module is enabled by default. Disable it, and enable the prefork module :
sudo a2dismod mpm_event sudo a2enmod mpm_prefork
Restart Apache:
sudo service apache2 restart
The Worker Module
The Worker Module is a hybrid Prefork, multi-threaded, multi-processor module. It’s similar to Prefork, but each child is multi-threaded.
Open
/etc/apache2/mods-available/mpm_worker.conf
in your text editor and edit the values as needed. The following are the default values:- File: /etc/apache2/mods-available/mpm_worker.conf
# worker MPM # StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadLimit: ThreadsPerChild can be changed to this maximum value during a # graceful restart. ThreadLimit can only be changed by stopping # and starting Apache. # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestWorkers: maximum number of threads # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_worker_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
On Ubuntu 18.04, the event module is enabled by default. Disable it, and enable the worker module :
sudo a2dismod mpm_event sudo a2enmod mpm_worker
Restart Apache:
sudo service apache2 restart
The Event Module
The Event Module is similar to the Worker Module except each thread has a dedicated listener so that threads are not locked in wait. As of Apache 2.4 the Event Module is considered stable. For versions before 2.4, use the Worker Module.
If you choose to keep the event module enabled, open
/etc/apache2/mods-available/mpm_event.conf
in your text editor and edit the values as needed. The following are the default values:- File: /etc/apache2/mods-available/mpm_event.conf
# event MPM # StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_event_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Restart Apache:
sudo service apache2 restart
Configure Virtual Hosting
Apache supports name-based virtual hosting, which allows you to host multiple domains on a single server with a single IP. Although there are different ways to set up virtual hosts, the method below is recommended.
Disable the default Apache virtual host:
sudo a2dissite 000-default.conf
Create an
example.com.conf
file in/etc/apache2/sites-available
with your text editor, replacing instances ofexample.com
with your own domain URL in both the configuration file and in the file name:- File: /etc/apache2/sites-available/example.com.conf
1 2 3 4 5 6 7 8
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html/ ErrorLog /var/www/example.com/logs/error.log CustomLog /var/www/example.com/logs/access.log combined </VirtualHost>
Repeat this process for any other domains you host.
Note
If you would like to enable Perl support, add the following lines above the closing
</VirtualHost>
tag:- File: /etc/apache2/sites-available/example.com.conf
1 2
Options ExecCGI AddHandler cgi-script .pl
Create directories for your websites and websites’ logs, replacing
example.com
with your own domain information:sudo mkdir -p /var/www/example.com/public_html sudo mkdir /var/www/example.com/logs
Create a simple page for your
index.html
.- File: /var/www/example.com/public_html/index.html
1 2 3 4 5 6 7 8 9
<!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World! This is my sample website with Apache on Ubuntu!</h1> </body> </html>
Enable the site:
sudo a2ensite example.com.conf
Restart Apache:
sudo service apache2 restart
Visit your site by navigating to your domain name in the web browser.
Apache Mods and Scripting
Install Apache Modules
One of Apache’s strengths is its ability to be customized with modules. The default installation directory for Apache modules is the /etc/apache2/mods-available/
directory.
List available Apache modules:
sudo apt-cache search libapache2*
Install any desired modules:
sudo apt-get install [module-name]
All mods are located in the
/etc/apache2/mods-available
directory. Edit the.conf
file of any installed module if needed, then enable the module:sudo a2enmod [module-name]
To disable a module that is currently enabled:
a2dismod [module-name]
Optional: Install Support for Scripting
The following commands install Apache support for server-side scripting in Perl, Python, and PHP. Support for these languages is optional based on your server environment.
To install:
Perl support:
sudo apt-get install libapache2-mod-perl2
Python support:
sudo apt-get install libapache2-mod-python
PHP support:
sudo apt-get install libapache2-mod-php7.2 php7.2 php-pear
Check Server Status
You can check your Apache web server status with the command:
sudo systemctl status apache2
The output will look similar to the following:
● 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 Thu 2020-02-27 14:28:23 EST; 1h 19min ago
Main PID: 4296 (apache2)
Tasks: 55 (limit: 1108)
CGroup: /system.slice/apache2.service
├─4296 /usr/sbin/apache2 -k start
├─4300 /usr/sbin/apache2 -k start
└─4301 /usr/sbin/apache2 -k start
From here you can see that the server is running successfully. However, if something isn’t working correctly, you can check the logs for errors. The logs locations are defined for each virtual host you set up in Configure Virtual Hosting.
Typically they will be at
/var/www/example.com/logs/error.log
and/var/www/example.com/logs/access.log
whereexample.com
is your domain name.
Controlling Apache
You can control the server in the following ways.
Stopping the server when it’s running:
sudo systemctl stop apache2
Start the server when it’s stopped:
sudo systemctl start apache2
Stop and start the server when it’s running:
sudo systemctl restart apache2
Reload the configurations while the server is running without stopping it:
sudo systemctl reload apache2
You can disable Apache so that it stops and doesn’t restart again when rebooting the system:
sudo systemctl disable apache2
To re-enable Apache if it’s been disabled. This will also enable it to restart when the system reboots:
sudo systemctl enable apache2
Optional: Firewall
Depending on your firewall configuration, you may need to modify your settings to allow access to web ports. A common firewall for Ubuntu is UFW.
If you had UFW installed before you installed Apache, Apache will have registered with UFW during installation and provides some simple to use configurations.
To view these options, run the following command:
sudo ufw app list
Available applications: Apache Apache Full Apache Secure OpenSSH
To view what these different configurations do, run this command:
sudo ufw app info 'Apache'
Replace
Apache
withApache Full
orApache Secure
to see information about those applications. Below is a table summary.Profile Title Ports Apache Web Server 80/tcp Apache Full Web Server (HTTP,HTTPS) 80,443/tcp Apache Secure Web Server (HTTPS) 443/tcp To enable a profile use the following command:
sudo ufw allow 'Apache'
Rules updated Rules updated (v6)
Verify the rules are updated with the following:
sudo ufw status
Status: active To Action From -- ----- ---- Apache ALLOW Anywhere Apache (v6) ALLOW Anywhere (v6)
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on