Ubuntu rocks!

How To Install Apache 2 On Ubuntu 20.04 LTS

It provides the steps required to install Apache 2 Web Server on Ubuntu 20.04 LTS.

Apache Web Server is among the popular web servers and widely-used to host static and PHP based websites. Most of the WordPress sites are being hosted on servers having Apache Web Server. This tutorial provides the steps required to install Apache 2 Web Server on Ubuntu 20.04 LTS. The steps required to install Apache 2 on other Ubuntu versions and Linux systems should be similar to the steps mentioned in this tutorial.

Prerequisites

This tutorial assumes that you have already installed Ubuntu 20.04 LTS desktop or server version either for local or production usage. You can follow Install Ubuntu 20.04 LTS Desktop, Install Ubuntu 20.04 LTS On Windows Using VMware, and Spin Up Ubuntu 20.04 LTS Server On Amazon EC2 to install Ubuntu 20.04 LTS. It also assumes that you have either root privileges or a regular user with sudo privileges.

In case you are installing Apache Web Server for production usage, it assumes that the ports 80 and 443 are publicly open. On AWS EC2, we can explicitly open these ports by updating the security group associated with the servers. Similar to EC2, DigitalOcean (Cloud Firewalls) and UpCloud (L3 firewall) also provide a firewall interface at an additional cost. On other cloud service providers, you may use the standard firewall i.e. UFW (ships by default with Ubuntu) to secure the server. In such cases, enable the appropriate apache profile based on your production usage.

Install Apache 2

This section provides the commands required to install Apache Web Server. Now, execute the below-mentioned commands to install Apache 2 on Ubuntu 20.04 LTS.

# Refresh the indexes
sudo apt-get update

# Install Apache
sudo apt-get install apache2

# Verify Apache
apache2 -version

# Output
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-04-13T17:19:17

# Autoclean
sudo apt-get autoclean

# Autoremove
sudo apt-get autoremove
# OR
sudo apt-get --purge autoremove

The above-mentioned commands install the latest version 2.4.41 of Apache 2 while writing this tutorial.

Install Apache 2 Modules

This section provides the commands to install the additional modules including headers and expires. Install the additional modules using the below-mentioned command.

# Install security
sudo apt-get install libapache2-mod-security2

# Enable additional modules
sudo a2enmod rewrite ssl security2 deflate expires headers

# Restart Apache 2
sudo systemctl restart apache2

Verify Installation

Now check the server status on the terminal as shown below.

# Server Status
sudo systemctl status apache2

# Output
apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-06-02 12:14:19 UTC; 5min ago Docs: https://httpd.apache.org/docs/2.4/ Process: 33493 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 33513 (apache2) Tasks: 6 (limit: 4622) Memory: 26.9M CGroup: /system.slice/apache2.service ├─33513 /usr/sbin/apache2 -k start ├─33514 /usr/sbin/apache2 -k start ├─33515 /usr/sbin/apache2 -k start ├─33516 /usr/sbin/apache2 -k start ├─33517 /usr/sbin/apache2 -k start └─33518 /usr/sbin/apache2 -k start Jun 02 12:14:19 ip-172-31-1-204 systemd[1]: Starting The Apache HTTP Server. Jun 02 12:14:19 ip-172-31-1-204 systemd[1]: Started The Apache HTTP Server.

The default www directory used by the Apache Web Server is /var/www/html. Now open the Apache’s Home Page in your favorite browser by using the server’s public IP address (http://xx.xx.xx.xx) or the DNS name assigned by the service provider. It should show the Home Page similar to Fig 1. It can also be accessed using http://localhost or http://127.0.01 in case it’s installed on a local desktop or server system.

Читайте также:  Настройка сетевых маршрутов linux

Basic Configuration

In this section, we will do some basic configuration specific to performance. These are optional and configure only if required.

Keep-Alive

Configure the keep-alive settings as mentioned below. The actual settings might vary based on your requirements.

# Open apache2 configuration
sudo nano /etc/apache2/apache2.conf


# Check the values of below mentioned options
KeepAlive On
MaxKeepAliveRequests 50
KeepAliveTimeout 5
MPM — Prefork — Optional

With the Prefork MPM, Apache Web Server works in the non-threaded mode which consumes more resources as compared to the Event MPM. The Event MPM is enabled by default in Apache 2 on Ubuntu 20.04 LTS. Also, enable Prefork MPM only if it’s really required. Configure the server process as shown below. Change the values based on requirements.

sudo nano /etc/apache2/mods-available/mpm_prefork.conf


StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0


# Save the settings

# Disable MPM Event
sudo a2dismod mpm_event

# Enable MPM Prefork
sudo a2enmod mpm_prefork
Production Mode

Update server tokens and signature as shown below. It must be done on production servers.

# Tokens and Signature
sudo nano /etc/apache2/conf-enabled/security.conf

ServerTokens Prod
ServerSignature Off


# Restart Apache 2
sudo systemctl restart apache2

These are the basic steps required to install Apache 2 and the basic configurations that can be applied to it.

Configure Firewall (Production Usage)

If your cloud service provider provides a firewall interface to configure the firewall, and you have configured your server to use the firewall interface provided by the service provider, open the ports 80 or 443 or both based on your server requirements. In case you are not using the firewall interface provided by the service provider and using UFW on your system, you can enable the appropriate profile as shown below.

Notes: It’s mandatory to assign Security Group to the AWS EC2 instances and we do not need to configure UFW for EC2 instances for most of the use cases.

# UFW Apache Profiles
sudo ufw app list

# Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

The Apache profiles can be used to configure UFW as listed below.

Читайте также:  Bonding in linux modes

Apache Profile — It opens only port 80 to allow unencrypted traffic.

Apache Full Profile — It opens port 80 and port 443 to allow unencrypted and TLS/SSL encrypted traffic.

Apache Secure Profile — It opens port 443 to allow TLS/SSL encrypted traffic.

Now activate the Apache Profile to open the ports 80 and 443 as shown below.

# Activate Apache Full Profile
sudo ufw allow 'Apache Full'

# Check Apache Profile
sudo ufw status

# Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)

We can see that the Apache Profile is activated and open to accept connections from anywhere.

Basic Commands

This section explains the basic commands available in Ubuntu, and specific to the Apache Web Server installed by us.

# Server Status
sudo service apache2 status
# OR
sudo systemctl status apache2

# Stop the Server
sudo service apache2 stop
# OR
sudo systemctl stop apache2

# Start the Server
sudo service apache2 start
# OR
sudo systemctl start apache2

# Re-start the Server
sudo service apache2 restart
# OR
sudo systemctl restart apache2

# Reload the Server
sudo service apache2 reload

# Enable Site
sudo a2ensite

# Disable Site
sudo a2dissite

# Enable Module
sudo a2enmod

# Disable Module
sudo a2dismod

We must always use the reload command to apply the site configurations without disrupting the active sessions.

Configure Virtual Host (Production Usage)

We have to configure the virtual host to use the domain name for production usage so that the Website or Application is publicly accessible using the domain name. This prevents the usage of IP addresses which are hard to remember. Also, we can use the same IP address to configure multiple virtual hosts. You can follow Configure Virtual Host On Apache to configure the websites for production usage.

We can use aliases on the local system instead of virtual hosts.

Secure Apache (Production Usage)

You can further secure the websites and web applications deployed on Apache using the SSL certificate. The most commonly used service for SSL certificates is Let’s Encrypt. You can also follow How To Install Let’s Encrypt For Apache On Ubuntu and Redirect HTTP to HTTPS on Apache to secure your websites using Let’s Encrypt.

Summary

This tutorial provided the steps required to install Apache Web Server on Ubuntu 20.04 LTS.

Источник

Install and Configure Apache

Apache is an open source web server that’s available for Linux servers free of charge.

In this tutorial we’ll be going through the steps of setting up an Apache server.

What you’ll learn

What you’ll need

  • Ubuntu Server 16.04 LTS
  • Secure Shell (SSH) access to your server
  • Basic Linux command line knowledge

Got everything ready? Let’s move on to the next step!

Originally authored by Aden Padilla

2. Installing Apache

To install Apache, install the latest meta-package apache2 by running:

sudo apt update sudo apt install apache2 

After letting the command run, all required packages are installed and we can test it out by typing in our IP address for the web server.

Читайте также:  Linux kernel and virtualbox

Apache-Installed

If you see the page above, it means that Apache has been successfully installed on your server! Let’s move on.

3. Creating Your Own Website

By default, Apache comes with a basic site (the one that we saw in the previous step) enabled. We can modify its content in /var/www/html or settings by editing its Virtual Host file found in /etc/apache2/sites-enabled/000-default.conf .

We can modify how Apache handles incoming requests and have multiple sites running on the same server by editing its Virtual Hosts file.

Today, we’re going to leave the default Apache virtual host configuration pointing to www.example.com and set up our own at gci.example.com .

So let’s start by creating a folder for our new website in /var/www/ by running

We have it named gci here but any name will work, as long as we point to it in the virtual hosts configuration file later.

Now that we have a directory created for our site, lets have an HTML file in it. Let’s go into our newly created directory and create one by typing:

cd /var/www/gci/ nano index.html 

Paste the following code in the index.html file:

      

I'm running this website on an Ubuntu Server server!

Now let’s create a VirtualHost file so it’ll show up when we type in gci.example.com .

4. Setting up the VirtualHost Configuration File

We start this step by going into the configuration files directory:

cd /etc/apache2/sites-available/ 

Since Apache came with a default VirtualHost file, let’s use that as a base. ( gci.conf is used here to match our subdomain name):

sudo cp 000-default.conf gci.conf 

Now edit the configuration file:

We should have our email in ServerAdmin so users can reach you in case Apache experiences any error:

ServerAdmin yourname@example.com 

We also want the DocumentRoot directive to point to the directory our site files are hosted on:

The default file doesn’t come with a ServerName directive so we’ll have to add and define it by adding this line below the last directive:

This ensures people reach the right site instead of the default one when they type in gci.example.com .

Now that we’re done configuring our site, let’s save and activate it in the next step!

5. Activating VirtualHost file

After setting up our website, we need to activate the virtual hosts configuration file to enable it. We do that by running the following command in the configuration file directory:

You should see the following output

Enabling site gci. To activate the new configuration, you need to run: service apache2 reload root@ubuntu-server:/etc/apache2/sites-available# 

To load the new site, we restart Apache by typing:

End result

Final

Now is the moment of truth, let’s type our host name in a browser. Hooray!

Further reading:

Источник

Оцените статью
Adblock
detector