Build your own web server

2022 May 30 See all posts


Build your own web server

In this article we are going to build a Web-Server based on Debian (Linux) OS, using Apache2. We will also configure and secure the SSH access, so that we can securely access our server from a remote desktop.

I'm not gonna explain why you'd like to build an on-premise web server (Privacy! 🔔). You can of course always google it!

Let's get right into the how-to build it 🏗️!

Requirements:

Steps:

  1. Set up the operating system (Linux)
  2. Install and configure the webserver (Apache)
  3. Configure secure ssh access to your server

Set up the operating system

Overview

You can use any Linux distro of preference for the OS, In this case I'll use Debian. It's well suited for web servers and it's relatively easy to get started with.

Similar options can be Ubuntu, Linux Mint, or for more experienced people Gentoo.

Start:

BIOS

Once your computer boots from USB, you'll go through the Debian installation process

Recommendations for installing Debian:

Debian OS

Congrats! You have now Debian OS up and running 🏃🏃‍♀️!

Install and configure the webserver

Overview

Let's start by updating the packages.
sudo apt update

–– If your account doesn't have sudo rights, switch to the root user by running su -

Install Apache2 by running
sudo apt install apache2
Verify installation by running
apache2 -version
Configure the Firewall settings (if it's running in your system)
sudo ufw allow 80/tcp # (default network port used to send and receive unencrypted web pages)

sudo ufw allow 443/tcp # (network port used to make secured and encrypted data - HTTPS)
Verify port settings
sudo ufw status
Verify Apache2 is active by running:
sudo systemctl status apache2

The response should similar to: Check Apache Status
Credits to tecmint.com for the image

If the server is not running, you can start it by running:

sudo systemctl start apache2 **or** sudo systemctl restart apache2
Get your server IP address (hostname) by running:
hostname -I

Access your web server! 👌

Open the browser and navigate to http://your-server-IP-address (e.g. http://192.173.43.21)

Web Server Homepage

Congrats! You now have your own web server ✨!

#3 - Secure SSH access to manage your server

Overview

I would like to ideally manage my server from a remote computer, thus let's configure it to have secure SSH access.

SSH Key access

It's recommended to enter your server by using SSH Keys instead of passwords since it's a more secure way to do so.

How to:

Switch to your local user profile that will be accessing the server
su username

It's best practice neither to use root nor admin users

Generate a new key pair
ssh-keygen -t rsa

It's recommended to add a catchphrase when generating the key since it adds an extra layer of security

Check that the public key was created successfully
ls ~/.ssh/id_*
Copy the key to your remote server
ssh-copy-id -i ~/.ssh/id_rsa.pub remote_username@your_server_ip_address
Validate that the key was added successfully
ssh remote_username@your_server_ip_address

SSH access configuration

Navigate to /etc/ssh/sshd_config and within the file:

Change 22 Port

Change port 22 to any non-default port: e.g. Port 20155

Disable Root logins

Set #PermitRootLogin as noPermitRootLogin no

Disable empty passwords

Set #PermitEmptyPasswords as PermitEmptyPasswords no

Enable Protocol 2

Add the line Protocol 2 to the file.

Limit for password attends

Set #MaxAuthTries to MaxAuthTries 3

or Disable password authentication altogether (Important -– Please make sure you already have SSH Key access before disabling it) — Set #PasswordAuthentication as PasswordAuthentication no

Now, restart SSH service to apply our changes
systemctl restart ssh

Let's finally test our changes!

SSH into your server using your keys
ssh remote_username@your_server_ip_address -p your_server_port_number

Log in into server

Woohoo! You can now SSH into your server 🔥 !