Blog

Simple Apache website configurations

Posted December 05 2024

This is just a small instruction set for creating a dynamic website with Apache2, this assumes you're using Linux and not Windows or MacOS, which would change the way you connect to the server.

Any snippets that contain: <content> (words inside <>) are to show content that need to be changed to the labels relevant to your website.

Connecting to the server

First you will need to securely connect into the server you want to host your website on.

Your server/vps provider will provide you with the username and password to access the server either on the page when you buy it or over email.

ssh <username>@<ip_address>

It will then ask you to input the password, do so and you will have access to the server.

Transferring project to the server

First you need to copy your website directory onto the server, this can either be done in the terminal with SCP or in the file manager through SFTP.

Via Terminal / SCP

Open a new tab in your terminal so you're on your client instead of the server.

Navigate to the parent directory of the project folder:

Execute:

scp -r /<project_folder> <username>@<ip_address>:/var/www/html/

Via File Manager / SFTP

Open your file manager, in the left side there is a network tab, open that and depending on the file manager there will be a Network/Other locations address input somewhere, on that input:

sftp:/<ip_address>

Once you are in the server navigate to: /var/www/html/ and paste the folder of your project.

Setting up Apache

sudo apt-get install apache2 certbot

This installs Apache2 and Certbot, the server and certificate tool respectively

sudo a2enmod ssl proxy

Enables SSL and proxy for the website.

Navigate to: /etc/apache2/sites-available/ and then execute this command:

sudo nano <project_name>.conf

In the file you just created and are now editing paste this in and change the tags

<VirtualHost *:80>
    ServerName http://<domain>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:<port>/
    ProxyPassReverse / http://127.0.0.1:<port>/
    DocumentRoot /var/www/html/<project_folder>
    ErrorLog /var/www/html/<project_folder>/<project_name>-error.log
    CustomLog /var/www/html/<project_folder>/<project_name>-access.log combined
</VirtualHost>

DocumentRoot needs to point to the folder you have your website in, which should be in /var/www/html, as advised by Apache2.

To save and exit the file press CTRL+X and then Y

Now execute this

sudo a2ensite <project_name>.conf

This enables the site to actually be used by Apache

Setting up Systemd

Now navigate to: ~/.config/systemd/user/

If these folders don't exist, you will need to create them by using sudo mkdir <folder_name in each subdirectory.

Now execute:

nano <project_name>.service
[Unit]
Description=<project_name> 
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/www/html/<project_folder>
ExecStart=/usr/bin/python3 /var/www/html/<project_folder>/<python_file>.py
Restart=always

[Install]
WantedBy=default.target

Save and exit

Now execute these:

systemctl --user daemon-reload

Reloads services

systemctl --user enable <project_name>.service

Enables the website

sudo loginctl enable-linger <username>

Allows the code to continue to run without being logged in.

Creating an SSH key for easier access

When connecting to the server, it is significantly quicker if you create an SSH key so you don't have to input the password every time you want to connect.

So here is how you create an SSH key:

On your client:

ssh-keygen

You can just go through the questions by pressing Enter for each one, they aren't required to be different to the default.

ssh-copy-id <username>@<ip_address>

Now type yes to continue connecting, as will likely be asked by the prompt.

Next will ask you for the password of the username you used above, enter it and continue.

Now close the connection with exit, and then again use:

ssh <username>@<ip_address>

If it prompts you again, type yes and continue

Now to disable using ssh via password usage (this will not allow anyone to connect without an SSH key, removing exposure to brute-force attacks)

sudo nano /etc/ssh/sshd_config

Scroll down until you find

PasswordAuthentication yes

Change this to:

PasswordAuthentication no

To save and exit CTRL+X y

To actually put this change into effect

sudo systemctl restart ssh
exit

Now your server will only respond to attempts with SSH keys instead of allowing a password to be inputted.