DEV Community

Leandro Nuñez
Leandro Nuñez

Posted on

Hosting Your Own Website with Apache on Debian/Ubuntu and Securing It with Let's Encrypt

Introduction:

Hey there web enthusiasts!

Are you eager to take control of your online presence and host your own website with a personalized domain name?

Look no further! In this step-by-step guide, we'll show you how to set up your very own website using the Apache web server on the latest Debian or Ubuntu LTS versions.

Plus, we'll walk you through the process of securing your site with the latest version of Let's Encrypt SSL certificates.

Let's get started!

Step 1: Installing Apache:

First things first, let's install the Apache web server on your Debian or Ubuntu system.

Open your terminal and type the following command:

sudo apt update
sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring Apache:

Once Apache is installed, it's time to configure it.
You'll need to set up your virtual hosts to serve your website correctly.

We'll assume your domain name is "yourdomain.com" for this example. Replace it with your actual domain.

Create a new virtual host file for your website:

sudo nano /etc/apache2/sites-available/yourdomain.com.conf
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Enable the virtual host and restart Apache:

sudo a2ensite yourdomain.com.conf
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Step 3: Setting Up Your Website:

Create the directory to store your website files:

sudo mkdir -p /var/www/yourdomain.com/public_html
Enter fullscreen mode Exit fullscreen mode

Next, upload your website files to this directory using your favorite FTP client or by SCP.

Step 4: Installing Let's Encrypt:

Now, let's secure your website with Let's Encrypt SSL certificates to protect your visitors' data.
We'll use the Certbot tool to do this.

sudo apt install certbot python3-certbot-apache
Enter fullscreen mode Exit fullscreen mode

Step 5: Obtaining SSL Certificates:

Run Certbot to obtain SSL certificates for your domain:

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Certbot will guide you through the process, and once it's done, your website will be secured with SSL!

Step 6: Automating Certificate Renewal:

Let's Encrypt SSL certificates are valid for a limited time, so it's essential to automate the renewal process. Luckily, Certbot takes care of that for us.

To set up a cron job for automatic renewal, run the following command:

sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line to the file and save it:

30 2 * * 1 /usr/bin/certbot renew --quiet
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Congratulations!
You've successfully hosted your own website with a custom domain name using Apache on Debian/Ubuntu and secured it with Let's Encrypt SSL certificates.

Now you have full control over your online presence and can provide a secure experience to your visitors. Keep exploring and learning to enhance your website further.

Happy hosting!

Top comments (0)