Skip to content

Instantly share code, notes, and snippets.

@RestoreMonarchy
Last active November 9, 2019 11:10
Show Gist options
  • Save RestoreMonarchy/a46016e3f075c50f9be8d25bc44bdf44 to your computer and use it in GitHub Desktop.
Save RestoreMonarchy/a46016e3f075c50f9be8d25bc44bdf44 to your computer and use it in GitHub Desktop.
Hosting PHP website on Apache2 Debian 9

Introduction

All the next steps where written while being tested on the Linux Debian 9 dedicated server.

Requirements

Install sudo

Before we begin we have to install sudo because we'll need it in the next steps, so let's update and upgrade the operating system with the following commands

apt update
apt upgrade

Then to install sudo execute the command

apt install sudo

Once the commands complete, sudo is installed.

Now if you want to run sudo as a non-root user, you have to use the following command

usermod -aG sudo USERNAME

Where USERNAME is the name of the user.

Install curl

To install curl, just execute the command

sudo apt install curl

Install ufw

To install ufw, execute the command

sudo apt-get install ufw

Install Apache Web Server

In order to install Apache Web Server, install apache2 package

sudo apt install apache2

Now you need to adjust the Firewall

sudo ufw allow 'WWW Full'

This should open both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)

To verify if apache2 service is running, use this command

sudo systemctl status apache2

Configure Apache Web Server

To host a website on your domain, first in /var/www create a new folder with your domain address.
Then in this folder create public, logs and crt folders and in the public, put the website's files.
In crt upload your certificate files and logs leave empty, because it will be used to store website's logs.

Now in the /etc/apache2/sites-available directory, create a new .conf file with a name of your domain. Open the file and paste this:

<VirtualHost *:443>
	SSLEngine on
		SSLCertificateFile /var/www/YOURDOMAIN/crt/certificate.crt
		SSLCertificateKeyFile /var/www/YOURDOMAIN/crt/private.key
		SSLCertificateChainFile /var/www/YOURDOMAIN/crt/ca_bundle.crt
	ServerName YOURDOMAIN
	ServerAdmin webmaster@YOURDOMAIN
	DocumentRoot /var/www/YOURDOMAIN/public
	ErrorLog /var/www/YOURDOMAIN/logs/error.log
</VirtualHost>

<VirtualHost *:80>
	ServerName YOURDOMAIN
	DocumentRoot /var/www/YOURDOMAIN/public
	Redirect permanent / https://YOURDOMAIN
</VirtualHost>

When you finish configuring, execute the command to activate the site

sudo a2ensite YOURDOMAIN.conf

Then use the commands to validate the changes and restart apache2 service

sudo apache2ctl configtest
sudo systemctl restart apache2

If you are getting a message Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server configuration, use this command to enable the SSLEngine module and then restart apache2 to apply changes

sudo a2enmod ssl

Install PHP

To install PHP latest stable version, run the command

sudo apt install php

Resources

Install sudo https://www.techrepublic.com/article/how-to-install-sudo-on-a-debian-minimal-server/
Install curl https://www.cyberciti.biz/faq/howto-install-curl-command-on-debian-linux-using-apt-get/
Install ufw https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server
Install apache2 https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-debian-9 Install PHP https://tecadmin.net/install-php-debian-9-stretch/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment