Installing Apache2 and HTTPS on Ubuntu 22.04 LTS
In this simple step-by-step guide, we see how to Install Apache2 on Ubuntu 22.04 LTS Jammy Jellyfish and configure the Web Server with SSL HTTPS. The tutorial is designed for users with basic computer and Linux knowledge.
Introduction
Apache is one of the most popular web servers in the world, known for its reliability and ease of configuration. In this guide, we will see how to install and configure Apache on Ubuntu 22.04, one of the latest versions of this popular Linux distribution.
Prerequisites
Before you start, make sure you have:
- A server or Virtual Machine with Ubuntu 22.04 installed
Rootaccess orsudoprivileges- A stable Internet connection
Step 1: Update the System
First, it is important to update the Ubuntu 22.04 LTS Jammy Jellyfish packages to make sure you have the latest and safest versions. Open the terminal and type:
sudo apt update
sudo apt upgradeThese commands update the package index and install available updates.
Step 2: Install Apache2 on Ubuntu 22.04
To install Apache2 on Ubuntu 22.04, use the apt command:
sudo apt install apache2The system will download and install Apache and its dependencies.
Step 3: Verify the Installation
To verify that Apache has been properly installed and that the service is active, you can use the following command:
sudo systemctl status apache2You should see output similar to "active (running)." You can also check by opening a Web browser and navigating to your server's IP address. You should see the Apache welcome page.
Step 4: Configuring Virtual Hosts.
Virtual Hosts allow you to host multiple websites on a single Apache server. To configure a Virtual Host, follow these steps:
Create the directory for the files that will be served by Apache
mkdir /var/www/html/example.com.Create a Virtual Host configuration file with an appropriate name, such as
example.com.conf, in the/etc/apache2/sites-availabledirectory.nano /etc/apache2/sites-available/example.com.Add the following content to the configuration file, replacing
example.comwith your domain name:<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html/example.com <Directory /var/www/html/example.com> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>Enable Virtual Host using the
a2ensitecommand or by creating a symbolic link to the configuration file in the/etc/apache2/sites-enableddirectory:sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/example.com.confRestart Apache to apply the changes:
sudo service apache2 restart
HTTPS with free SSL certificates Let's Encrypt
Now that we have installed Apache on Ubuntu 22.04, thanks to the excellent Let's Encrypt, you can generate SSL/TLS certificates, which are universally recognized, secure and free
We need to configure the DNS name of the website we are going to host so that it reaches the IP of our server
DNS configuration example
To configure DNS, we need to log into our provider's panel and register a name like this
example.com A 1.1.1.1We install Let's Encrypt and generate the SSL and TLS certificates
apt install certbot python3-certbot-apacheLet's create the Apache2 configuration
certbot --apacheWe test the renewal of certificates
certbot renew --dry-runWe check that the scheduled Cron process for renewal is present
cat /etc/cron.d/certbotIf there were no problems resolving our domain, our virtual hosts will respond in HTTPS, returning the famous "green padlock" to us on the browser
Apache Configurations
The configuration of Apache is mainly done through files in /etc/apache2/. Here are some of the most important files:
/etc/apache2/apache2.conf: The main configuration file./etc/apache2/sites-available/: Directory where the configurations of available sites are located./etc/apache2/sites-enabled/: Directory where the configurations of active sites are located.
To enable a site, use the a2ensite command followed by the name of the site configuration file. To disable it, use a2dissite.
Managing the Apache service
You can use the following commands to manage the Apache service:
sudo service apache2start: starts the Apache servicesudo service apache2stop: stops the Apache servicesudo service apache2 restart: restarts the Apache servicesudo systemctl enable apache2: enables Apache to start automatically at system startupsudo systemctl disable apache2: disables the automatic startup of Apache at system startup
Useful resources
- Official Apache documentation: https://httpd.apache.org/docs/
Frequently Asked Questions (FAQ)
1. How can I restart Apache?
Use the command sudo service apache2 restart.
2. How can I view the Apache logs?
Apache logs are located in the /var/log/apache2/ directory. The main files are access.log and error.log.
3. Can I install additional modules for Apache?
Yes, you can install additional modules using the command sudo apt install module-name. Then enable the module with a2enmod module-name and restart Apache.
Conclusion
By following these simple steps, we have installed Apache on Ubuntu 22.04. If you need further assistance or want to optimize your configuration, please feel free to contact us. Our team of Apache experts is ready to help you.