This document is free text: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
Apache 2 Installation, configuration, sample sites, enablement of PHP, SSL etc on Ubuntu 24.04 (and 22.04) and Debian 12 (and 11) Servers
Based on the book Mastering Ubuntu Server 2nd Ed. by Jay LaCroix. This book hes introduced me to Ubuntu Server and I have to thank him for this excellent book.
srv1.386387.xyz, srv2.386387.xyz and srv3.386387.xyz all have the ip of my server
sudo apt update
sudo apt install apache2 --yes
It must be working with the test page
systemctl status apache2
sudo nano /var/www/html/index.html
Configuration files for different sites exist as .conf files in /etc/apache2/sites-available directory
sudo nano /etc/apache2/apache2.conf
/etc/apache2/sites-available
sudo nano /etc/apache2/sites-available/000-default.conf
For virtual hosts we need to create a new conf as say 000-virtual-hosts.conf
sudo nano /etc/apache2/sites-available/000-virtual-hosts.conf
Sample content for 2 virtual hosts
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
ServerAlias srv1
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv1.386387.xyz-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv2.386387.xyz
ServerAlias srv2
DocumentRoot /var/www/srv2
ErrorLog ${APACHE_LOG_DIR}/srv2.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv2.386387.xyz-access.log combined
</VirtualHost>
sudo a2ensite 000-virtual-hosts.conf
We can disable it again whenever we want
sudo a2dissite 000-virtual-hosts.conf
We need to reload Apache whenever we enable or disable a site
sudo systemctl reload apache2
Remember to copy sites' pages on DocumentRoot Directories: create /var/www/srv1 and /var/www/srv2 and fill them with htmls
apt search libapache2-mod
then can be disabled by a2dismod
sudo apache2 -l
Sudo is necessary for Debian
sudo a2enmod
sudo a2enmod proxy
sudo a2dismod proxy
sudo systemctl restart apache2
sudo a2enmod ssl
sudo systemctl restart apache2
sudo mkdir /etc/apache2/certs
sudo openssl req -x509 -nodes -days 730 -newkey rsa:2048 -keyout \
/etc/apache2/certs/srv1.key -out /etc/apache2/certs/srv1.crt
You need to answer all the questions, default values OK for a test site
sudo openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr
sudo nano /etc/apache2/sites-available/000-virtual-ssl.conf
Fill as below:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName srv1.386387.xyz:443
ServerAdmin webmaster@386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv.386387.xyz-access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/certs/srv1.crt
SSLCertificateKeyFile /etc/apache2/certs/srv1.key
<FilesMatch ".(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
sudo a2ensite 000-virtual-ssl.conf
Please consider, your browser will give an error/warning message because the certificate is self signed.
sudo systemctl reload apache2
First we need to enable rewrite mode
sudo a2enmod rewrite.load
sudo nano /etc/apache2/sites-available/000-virtual-hosts.conf
last 3 lines to be added
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1-error.log
CustomLog ${APACHE_LOG_DIR}/srv1-access.log combined
#redirection
RewriteEngine on
RewriteCond %{SERVER_NAME} =srv1.386387.xyz
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
sudo systemctl restart apache2
sudo apt install php libapache2-mod-php
sudo apt install php-mysql
Restart apache
sudo systemctl restart apache2
Create a test file for PHP
sudo nano /var/www/srv1/info.php
Fill as below:
<?php
phpinfo();
Test your page
http://srv1.386387.xyz/info.php
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv1.386387.xyz-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv2.386387.xyz
DocumentRoot /var/www/srv2
ErrorLog ${APACHE_LOG_DIR}/srv2.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv2.386387.xyz-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv3.386387.xyz
DocumentRoot /var/www/srv3
ErrorLog ${APACHE_LOG_DIR}/srv3.386387.xyz-error.log
CustomLog ${APACHE_LOG_DIR}/srv3.386387.xyz-access.log combined
</VirtualHost>
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@386387.xyz
ServerName srv3.386387.xyz
DocumentRoot /var/www/localhost
ErrorLog ${APACHE_LOG_DIR}/localhost-error.log
CustomLog ${APACHE_LOG_DIR}/localhost-access.log combined
</VirtualHost>
Otherwise redirects to https. You are going to need to enable rewrite module with:
sudo a2enmod rewrite
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
DocumentRoot /var/www/srv1
# Force redirect to HTTPS unless the request is for Let's Encrypt
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
<Directory "/var/www/srv1">
Options None
AllowOverride None
</Directory>
ErrorLog ${APACHE_LOG_DIR}/public_unencrypted.error.log
</VirtualHost>
Rewrite module is needed again.
<VirtualHost *:80>
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
ServerAlias www.386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1-error.log
CustomLog ${APACHE_LOG_DIR}/srv1-access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =srv1.386387.xyz [OR]
RewriteCond %{SERVER_NAME} =www.386387.xyz
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
<Directory "/var/www/srv1">
Require ip 195.174.209.24
Require ip 138.199.28.46
</Directory>
ServerAdmin postmaster@386387.xyz
ServerName srv1.386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1-error.log
CustomLog ${APACHE_LOG_DIR}/srv1-access.log combined
</VirtualHost>
Assume that we have a program on server which runs a web server, serving some content at some specific port and only allows connections from localhost. That means, we cannot access it from other computers.
Apache allows us using it as a reverse proxy. That way we can connect that web server using apache.
Rspamd is a good example of that kind of a program. It runs a web server at port 11334, and only allows connections from the computer itself.
We need to enable 2 Apache mods for the configuration:
a2enmod proxy_http
a2enmod rewrite
And our configuration:
<VirtualHost *:80>
<Location /reverse>
Require all granted
</Location>
RewriteEngine On
RewriteRule ^/reverse$ /reverse/ [R,L]
RewriteRule ^/reverse/(.*) http://localhost:11334/$1 [P,L]
ServerAdmin webmaster@386387.xyz
ServerName srv1.386387.xyz
DocumentRoot /var/www/srv1
ErrorLog ${APACHE_LOG_DIR}/srv1-error.log
CustomLog ${APACHE_LOG_DIR}/srv1-access.log combined
</VirtualHost>
You can use free, autorenewing SSL certificates from Letsencrypt.org with Certbot tool from EFF.
Check it out at my CertbotOnDebianUbuntu Tutorial.