Install Apache Webserver With SSL on Ubuntu 22.04

Page content

Apache is a highly popular web server powering a significant percentage of websites worldwide. It is free and open source. The Apache server was introduced in 1995. It is maintained by the Apache Server Foundation.

Apache webserver is highly flexible and can be easy to manage. Functionalities can be easily turned on and off using Apache modules. You can aslo enable the .htaccess file on Apache Web Server which can be used to control things like compression, security, caching and more settings.

Install Apache Webserver on Ubuntu 22.04

To install the Apache web server on Ubuntu 22.04, you can use the following steps. Please note that these instructions assume you have administrative (sudo) privileges on your Ubuntu system.

  1. Update Package Lists: Open a terminal and update the package lists to ensure you have the latest information about available packages:

    sudo apt update
    
  2. Install Apache: Use the following command to install the Apache web server:

    sudo apt install apache2
    
  3. Start Apache: Once the installation is complete, start the Apache service:

    sudo systemctl start apache2
    
  4. Enable Apache to Start on Boot: To ensure Apache starts automatically when the system boots, enable the service:

    sudo systemctl enable apache2
    
  5. Check Apache Status: Verify that Apache is running without any issues by checking its status:

    sudo systemctl status apache2
    

    If Apache is running, you should see an active (running) status.

  6. Configure Firewall: If you have a firewall enabled, you may need to allow HTTP traffic. The following command allows incoming traffic on the default HTTP port (80):

    sudo ufw allow 80
    

    If you’re using a different firewall management tool, make sure to adjust the rules accordingly.

  7. Access Apache Default Page: Open a web browser and enter your server’s IP address or hostname in the address bar. If Apache is installed correctly, you should see the default Apache landing page.

    http://your_server_ip
    

    Replace your_server_ip with the actual IP address of your server.

That’s it! You have successfully installed and configured the Apache web server on Ubuntu 22.04. You can now start placing your website files in the default document root directory, which is typically located at /var/www/html/. Add an index.html file to the root folder and try loading your server ip.

Add Virtual Host File and Configure Apache Server

Creating a Virtual Host (vhost) file in Apache on Ubuntu 22.04 involves defining separate configurations for different websites or applications. Here are the steps to create a vhost file:

1. Create a Vhost Configuration File:

  1. Navigate to the Apache sites-available directory:

    cd /etc/apache2/sites-available
    
  2. Create a new vhost configuration file: Use a text editor (such as nano or vim) to create a new file for your virtual host configuration. Replace your_domain.conf with a meaningful name for your domain:

    sudo nano your_domain.conf
    

2. Configure the Vhost:

Inside the vhost file, add the necessary configurations. Here’s a basic example:

<VirtualHost *:80>
    ServerAdmin webmaster@your_domain
    ServerName your_domain
    DocumentRoot /var/www/your_domain/public_html

    <Directory /var/www/your_domain/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/your_domain_error.log
    CustomLog ${APACHE_LOG_DIR}/your_domain_access.log combined
</VirtualHost>

Make sure to replace your_domain with your actual domain name, and adjust the DocumentRoot to the path where your website files are located.

3. Save and Close the File:

  • For nano: Press Ctrl + X, then press Y to confirm, and press Enter to save.

  • For vim: Press Esc, then type :wq and press Enter to save and exit.

4. Enable the Vhost:

  1. Create a Symbolic Link: Create a symbolic link to the sites-enabled directory to enable the virtual host:

    sudo ln -s /etc/apache2/sites-available/your_domain.conf /etc/apache2/sites-enabled/
    
  2. Restart Apache: After enabling the virtual host, restart Apache to apply the changes:

    sudo systemctl restart apache2
    

5. Adjust Hosts File (Optional):

If your website is not live yet, you can test it locally by adding an entry to your /etc/hosts file. Open the file with a text editor:

sudo nano /etc/hosts

Add a line like this:

127.0.0.1   your_domain

Replace your_domain with the actual domain specified in your vhost configuration.

6. Test the Configuration:

Open a web browser and enter your domain (or the local address if using the hosts file) to see if your website is working:

http://your_domain

If everything is set up correctly, you should see your website.

Note: If you’re setting up an SSL-enabled vhost, additional configurations are needed. Ensure you have an SSL certificate installed and adjust the vhost file accordingly.

Install Free SSL With LetsEncrypt on Apache2 Ubuntu 22.04

To install a free SSL certificate on Apache2, you can use Let’s Encrypt, a widely used Certificate Authority that provides free SSL certificates. Here’s a step-by-step guide for installing a Let’s Encrypt SSL certificate on Apache2 running on Ubuntu 22.04:

Prerequisites:

  1. A Registered Domain:

    • Ensure that you have a registered domain name that points to the IP address of your Apache server.
  2. Apache2 Installed:

    • Apache should be installed and configured on your Ubuntu 22.04 server.

Steps to Install Let’s Encrypt SSL Certificate:

  1. Install Certbot: Begin by installing Certbot, the client used to obtain Let’s Encrypt certificates. Open a terminal and run the following commands:

    sudo apt update
    sudo apt install certbot python3-certbot-apache
    
  2. Obtain SSL Certificate: Run Certbot to obtain and install the SSL certificate. Replace your_domain with your actual domain name:

    sudo certbot --apache -d your_domain
    

    Follow the on-screen instructions to configure Certbot. You may be prompted to provide an email address for important account notifications and agree to the terms of service.

  3. Configure SSL Settings in Apache: Certbot will automatically configure Apache to use the SSL certificate. After the certificate is installed, Certbot will reload Apache to apply the changes.

  4. Verify SSL Configuration: To check the SSL configuration, visit your website using the https:// prefix:

    https://your_domain
    

    Replace your_domain with your actual domain name. If the SSL installation is successful, your website should load securely with a padlock icon in the browser’s address bar.

Automated Renewal:

Let’s Encrypt certificates are valid for 90 days. It’s important to set up automated renewal to ensure your SSL certificate stays valid. Certbot includes a cron job that runs twice a day to automatically renew certificates.

You can test the renewal process with:

sudo certbot renew --dry-run

If the dry run is successful, the renewal process is set up correctly.

That’s it! You’ve successfully installed a free SSL certificate on Apache2 using Let’s Encrypt. Your website is now accessible over HTTPS, providing a secure connection for your users.