Pular para o conteúdo principal

How to Install Apache HTTP Server on Ubuntu

Apache HTTP Server is a free, open-source web server software that is widely used for hosting websites. It is a powerful tool that can be used to serve dynamic web content such as PHP scripts and static files like HTML, CSS, and JavaScript. In this tutorial, we will walk you through the process of installing Apache HTTP Server on Ubuntu.

Prerequisites

Before we begin, make sure you have the following:

  • An Ubuntu server or desktop installation.
  • A user account on the system with sudo privileges.
  • Internet connectivity for downloading packages.

Step 1: Update the system

It is always a good idea to update your system before installing any new software. To do so, open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

These commands will update the package lists and install any available updates.

Step 2: Install Apache HTTP Server

To install Apache HTTP Server, run the following command in the terminal:

sudo apt install apache2

This command will download and install Apache HTTP Server along with all its dependencies.

Once the installation is complete, you can check the status of the Apache service by running:

sudo systemctl status apache2

If the service is running, you should see a message indicating that it is active and running.

Step 3: Configure the Firewall

By default, Ubuntu comes with a firewall called UFW (Uncomplicated Firewall) that is disabled. To enable UFW and allow incoming HTTP and HTTPS traffic, run the following commands:

sudo ufw enable
sudo ufw allow 'Apache'
sudo ufw allow 'Apache Full'

The first command enables UFW, while the second and third commands allow incoming traffic on ports 80 (HTTP) and 443 (HTTPS), respectively.

Step 4: Verify the Installation

To test if Apache HTTP Server is installed correctly, open a web browser and enter the IP address of your Ubuntu server or desktop. If Apache is running, you should see the default Apache page.

Alternatively, you can test the Apache installation from the terminal by running the following command:

curl http://localhost/

This command should return the HTML code of the default Apache page.

Step 5: Create a Virtual Host

If you plan on hosting multiple websites on your Ubuntu server, you might want to create virtual hosts. A virtual host is a way of hosting multiple websites on a single server by using different domain names or IP addresses.

To create a virtual host, first, create a directory to hold the website files:

sudo mkdir /var/www/example.com

Next, create a sample index.html file:

echo "Welcome to my website!" | sudo tee /var/www/example.com/index.html

Now, create a new virtual host configuration file:

sudo nano /etc/apache2/sites-available/example.com.conf

Add the following configuration:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Next, enable the new virtual host by creating a symbolic link from the configuration file to the sites-enabled directory:

sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/

Finally, reload the Apache service to apply the changes:

sudo systemctl reload apache2

Now, if you enter the domain name or IP address of your Ubuntu server in a web browser, it should display the sample index.html file you created earlier.

Conclusion

In this tutorial, we have shown you how to install and configure Apache HTTP Server on Ubuntu. We have also demonstrated how to create a virtual host to host multiple websites on a single server. With these steps, you should be able to host your own websites on Ubuntu with ease.