Apache: Tutorial & Best Practices

A web server that is used to host websites and web applications

Apache is a popular open-source web server that is widely used to host websites and web applications. It is known for its stability, performance, and flexibility, and is a good choice for many different types of web projects.

To install Apache on a Linux system, you will need to have root access or sudo privileges. The installation process will vary depending on the Linux distribution you are using. Here are the overview steps for installing Apache on a Linux system:

Update the package manager's list of available packages:

sudo apt update # (Debian/Ubuntu)
sudo yum update # (CentOS/Red Hat)

Install Apache using the package manager:

sudo apt install apache2 # (Debian/Ubuntu)
sudo yum install httpd   # (CentOS/Red Hat)

Once the installation is complete, start the Apache service:

sudo systemctl start apache2 # (Debian/Ubuntu)
sudo systemctl start httpd   # (CentOS/Red Hat)

Enable the Apache service to start automatically at boot:

sudo systemctl enable apache2 # (Debian/Ubuntu)
sudo systemctl enable httpd   # (CentOS/Red Hat)

The last step may not be required depending on your Linux distribution. Once Apache is installed and running, you can begin configuring it. The main configuration file for Apache is typically located at /etc/httpd/conf/httpd.conf (CentOS/Red Hat) or /etc/apache2/apache2.conf (Debian/Ubuntu). You can edit this file using a text editor like vi or nano.

Here are a few common Apache configuration tasks:

  • Set the server's name and port number: By default, Apache listens for requests on port 80. You can change the port number by modifying the Listen directive in the configuration file.

  • Set the server's document root: The document root is the directory where Apache looks for the files that make up your website. You can set the document root by modifying the DocumentRoot directive in the configuration file.

  • Create virtual hosts: Virtual hosts allow you to host multiple websites on a single Apache server. You can create a virtual host by adding a <VirtualHost> block to the configuration file and specifying the hostname and document root for the website.

  • Enable mod_rewrite: mod_rewrite is an Apache module that allows you to use rewrite rules to modify the URLs of your website. To enable mod_rewrite, you will need to uncomment the LoadModule rewrite_module line in the configuration file and restart Apache.

  • Enable .htaccess files: .htaccess files allow you to override certain Apache configuration settings for a specific directory or website. To enable .htaccess files, you will need to set the AllowOverride directive to All for the relevant directory or virtual host.

Make changes to /etc/apache2/ports.conf and add ports and server name:

Listen 80
Listen 443
NameVirtualHost *:80
ServerName myserver.com

Add sites to /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/:

ServerName myserver.com
ServerAlias www.myserver.com
DocumentRoot /var/www/myserver.com/

    Options None
    Order deny,allow
    Allow from all

Or let's redirect all traffic to https:

ServerName myserver.com
ServerAlias www.myserver.com
Redirect permanent / https://myserver.com/

Use this configuration for https:

ServerName myserver.com
ServerAlias www.myserver.com
ServerAdmin webmaster@myserver.com

DocumentRoot /var/www/myserver.com/

Options None
Order deny,allow
Allow from all

AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require expr %{REQUEST_URI} =~ m#^/.well-known/acme-challenge/#
Require expr %{REQUEST_URI} =~ m#^/apple-touch-icon.png#
Require valid-user

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
RewriteRule ^ index.php [QSA,L]

SSLEngine on
SSLCertificateFile    /etc/letsencrypt/live/myserver.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/myserver.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/myserver.com/fullchain.pem
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"

Use this to remove www from the URL:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.myserver\.comg$
RewriteRule ^/(.*)$ https://myserver.com/$1 [R=301,L]

If you want to set up a reverse proxy, make sure to enable the modules proxy and proxy_http. After that, use this configuration:

ServerName myserver.com
ServerAlias www.myserver.com
ProxyPass / http://serverttoforward.com/
ProxyPassReverse / http://servertoforward.com/

This is a configuration for WordPress:

ServerName myserver.com
ServerAlias www.myserver.com
ServerAdmin webmaster@myserver.com
DocumentRoot /usr/share/wordpress
Alias /wp/wp-content /var/lib/wordpress/wp-content
Alias /wp /usr/share/wordpress

Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Options FollowSymLinks
Order allow,deny
Allow from all
Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA