Follow along with the steps below to install Apache on your system, and learn the basics of using the HTTP server. These steps will take place on the command line, so open a terminal to get started.

  1. Install Apache via the apt package manager by executing the following commands.
    $ sudo apt update
    $ sudo apt install apache2
    
  2. Once it’s installed, you can use systemd’s systemctl commands to control the service.

    Enable or disable Apache from starting at system boot:

    $ sudo systemctl enable apache2
    OR
    $ sudo systemctl disable apache2
    

    Start or stop the Apache web server:

    $ sudo systemctl start apache2
    OR
    $ sudo systemctl stop apache

    We have a separate guide to explain the difference between restarting or reloading the Apache service on Ubuntu 20.04.

    $ sudo systemctl restart apache2
    OR
    $ sudo systemctl reload apache2
    
  3. If you are using ufw firewall on your system, you’ll need to allow port 80 for HTTP traffic and 443 for HTTPS if you plan to use SSL. This will allow outside traffic to access your website.
    $ sudo ufw allow http
    AND (if applicable)
    $ sudo ufw allow https
    
  4. You can test to make sure everything is working correctly by navigating to http://localhost your system. You should be greeted by the default Apache page, as seen below.

  5. With Apache up and running, we’re ready to configure our website. The default directory for our website’s files is /var/www/html. Move your files here, or begin by replacing the default index.html greeting page. In this example, we’ll just make a simple HTML document to see the changes reflected on the website.
    $ echo Apache on Ubuntu 20.04 > index.html
    $ sudo mv index.html /var/www/html
    

    To learn more about virtual hosts, which allow you to host multiple websites or change the configuration of your current one (such as the directory where files are stored, domain name, as well as the error logs, etc), check our guide on Apache virtual hosts.

  6. To set up SSL encryption using Let’s Encrypt, install the certbot utility with the following command.
    $ sudo apt install certbot python3-certbot-apache
    
  7. Configure the SSL certificate by executing the following command and going through the prompts that pop up. The last question will ask you if you want to redirect HTTP requests straight to HTTPS. It’s recommended that you opt for this. Obviously, for this to work the domain must point correctly to our publicly accessible server IP.
    $ sudo certbot --apache
Was this answer helpful? 0 Users Found This Useful (0 Votes)