Skip to main content

Apache2 Tutorial - HTTP, HTTPS, PHP, Apache2 Commands. Ubuntu Version

727 words·
Apache PHP Certbot Ubuntu Webserver

Apache2
#

Installation
#

# Update package index
sudo apt update

# Install Apache2
sudo apt install apache2 -y

Start & Enable
#

# Start Apache
sudo systemctl start apache2

# Stop Apache
sudo systemctl stop apache2

# Restart Apache
sudo systemctl restart apache2

# Reload Apache without restart
sudo systemctl reload apache2

# Enable service on boot
sudo systemctl enable apache2

Status & Logs
#

# Check status
sudo systemctl status apache2

# Error logs
sudo tail /var/log/apache2/error.log

# Access logs
sudo tail /var/log/apache2/access.log

Configuration
#

Main Configuration Files
#

# Main configuration
sudo vi /etc/apache2/apache2.conf

# Ports configuration
sudo vi /etc/apache2/ports.conf
# Test configuration
sudo apache2ctl configtest

Apache2 Domain Name
#

Define the domain name for the server in the main configuration file.

# Open main configuration
sudo vi /etc/apache2/apache2.conf

# Add domain name
ServerName jklug.work

# Reload Apache2
sudo systemctl restart apache2

# Check status
sudo systemctl status apache2

Otherwise the following notification shows up:

# Check status
sudo systemctl status apache2

# Shell output
...ubuntu apachectl[3279]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1.

Virtual Hosts Configurations
#

# Virtual hosts configurations / website configurations
/etc/apache2/sites-available/

# Enabled virtual hosts configurations
/etc/apache2/sites-enabled/
# Enable virtual host
sudo a2ensite website.conf

# Diable virtual host
sudo a2dissite website.conf

Modules
#

# Modules directory / list modules
sudo ls /etc/apache2/mods-available/

# Enabled modules
sudo ls /etc/apache2/mods-enabled/
# Enable module
sudo a2enmod ...

# Disable module
sudo a2dismod ...
# Enable SSL module
sudo a2enmod ssl

Configuration Fragments
#

# Available configurations
sudo ls /etc/apache2/conf-available

# Enabled configurations
sudo ls /etc/apache2/conf-enabled
# Enable configuration
a2enconf

# Disable configuration 
a2disconf

Directories
#

# Default webcontent directors
/var/www/html/

Apache2 Webserver
#

HTTP Website
#

Virtual Hoist Folder & Permissions
#

# Create directory for website
sudo mkdir -p /var/www/mywebsite.jklug.work

# Set owner
sudo chown -R www-data:www-data /var/www/mywebsite.jklug.work

# Set permissions
sudo chmod -R 755 /var/www/mywebsite.jklug.work

HTML Testsite
#

# Create HTML file
sudo vi /var/www/mywebsite.jklug.work/index.html
<!-- /var/www/mywebsite.jklug.work/index.html -->
<!DOCTYPE html>
<html>

<head>
	<title>jklug.work</title>
</head>

<body>
	<h1>Apache Test</h1>
</body>

</html>

Virtual Host Configuration
#

# Create virtual host configuration
sudo vi /etc/apache2/sites-available/mywebsite.jklug.work.conf

# /etc/apache2/sites-available/mywebsite.jklug.work.conf
<VirtualHost *:80>
    ServerName mywebsite.jklug.work
    ServerAlias www.mywebsite.jklug.work
    DocumentRoot /var/www/mywebsite.jklug.work
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Virtual Host
#

# Enable website
sudo a2ensite mywebsite.jklug.work.conf

# Disable default configuration
sudo a2dissite 000-default.conf

# Reload Apache
sudo systemctl reload apache2

HTTPS Website
#

Certbot
#

# Install Certbot
sudo apt install certbot -y

# Stop Apache2
sudo systemctl stop apache2

# Create certificate
sudo certbot certonly --standalone -d mywebsite.jklug.work

# Start Apache2
sudo systemctl start apache2

Note: The --standalone plugin temporarily starts a web server for certificate validation and then stops it.


Folder & Permissions
#

# Create directory for website
sudo mkdir -p /var/www/mywebsite.jklug.work

# Set owner
sudo chown -R www-data:www-data /var/www/mywebsite.jklug.work

# Set permissions
sudo chmod -R 755 /var/www/mywebsite.jklug.work

HTML Testsite
#

# Create HTML file
sudo vi /var/www/mywebsite.jklug.work/index.html
<!-- /var/www/mywebsite.jklug.work/index.html -->
<!DOCTYPE html>
<html>

<head>
	<title>jklug.work</title>
</head>

<body>
	<h1>Apache Test</h1>
</body>

</html>

Virtual Host Configuration
#

# Create virtual host configuration
sudo vi /etc/apache2/sites-available/mywebsite.jklug.work.conf

# /etc/apache2/sites-available/mywebsite.jklug.work.conf
<VirtualHost *:80>
    ServerName mywebsite.jklug.work
    ServerAlias www.mywebsite.jklug.work
    Redirect permanent / https://mywebsite.jklug.work/ # Redirect HTTP to HTTPS
</VirtualHost>

<VirtualHost *:443>
    ServerName mywebsite.jklug.work
    ServerAlias www.mywebsite.jklug.work
    DocumentRoot /var/www/mywebsite.jklug.work

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/mywebsite.jklug.work/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mywebsite.jklug.work/privkey.pem

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Virtual Host
#

# Enable website
sudo a2ensite mywebsite.jklug.work.conf

# Disable default configuration
sudo a2dissite 000-default.conf

# Reload Apache
sudo systemctl reload apache2

Enable SSL Module
#

# Enable SSL module
sudo a2enmod ssl

# Reload Apache
sudo systemctl reload apache2

PHP Website
#

Install PHP and PHP Module
#

# Update package index
sudo apt update

# Install PHP and Apache2 PHP module
sudo apt install php libapache2-mod-php -y
# PHP settings: Find php.ini location
php --ini | grep 'Loaded Configuration File'

# Shell output:
Loaded Configuration File:         /etc/php/8.1/cli/php.ini

Adjust Apache config
#

Adjust Apache2 configuration to prefer PHP Files.

# Open dir module
sudo vi /etc/apache2/mods-enabled/dir.conf
  • Set index.php on the beginning of the list, it should look like this:
# /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Create PHP File
#

# Create PHP file
sudo vi /var/www/mywebsite.jklug.work/index.php
# /var/www/mywebsite.jklug.work/index.php
<?php
phpinfo();
?>

Note: For security reasons use phpinfo only for testing purposes.


Restart Apache
#

# Restart Apache2
sudo systemctl restart apache2

# Check status
sudo systemctl status apache2