Setting up an Apache server on Ubuntu is a foundational skill for anyone involved in web development or server administration. Apache HTTP Server is renowned for its flexibility, reliability, and extensive module library, making it the go-to choice for hosting websites worldwide. This guide will walk you through the process of installing and configuring Apache on Ubuntu, ensuring your server is robust, secure, and optimized for performance.
Prerequisites
Before diving into the Apache server setup on Ubuntu, ensure you have the following:
- Ubuntu Server: A server running Ubuntu. This guide is tailored for Ubuntu, and while the core principles might apply to other distributions, specific commands and configurations are Ubuntu-centric.
- Sudo Privileges: Access to a user account with
sudo
privileges. This is essential for executing administrative commands required for installation and configuration. - Basic Firewall: A firewall configured to protect your server. UFW (Uncomplicated Firewall) is a common choice on Ubuntu, and we’ll cover how to adjust it for Apache.
If you haven’t already set up these prerequisites, it’s highly recommended to follow a guide on initial server setup for Ubuntu to secure your server before proceeding.
Step 1 — Installing Apache on Ubuntu
The first step in setting up your Apache server on Ubuntu is the installation itself. Ubuntu’s repositories include Apache, simplifying the process with standard package management tools.
-
Update Package Index: Begin by updating your package index. This ensures you’re installing the latest versions of packages and their dependencies.
sudo apt update
-
Install Apache2 Package: With the package index updated, install the
apache2
package.sudo apt install apache2
This command will install Apache and all necessary dependencies. You’ll be prompted to confirm the installation; proceed to complete it.
Step 2 — Configuring the Firewall for Apache
Once Apache is installed, the next crucial step in the Apache Server Setup Ubuntu process is to configure your firewall to allow web traffic. If you’re using UFW, as recommended, you need to adjust the rules to permit external access to Apache’s default ports.
-
List UFW Application Profiles: Apache registers itself with UFW during installation, providing predefined application profiles. To see these profiles, use:
sudo ufw app list
This command will display the available Apache profiles:
Output Available applications: Apache Apache Full Apache Secure OpenSSH
These profiles are:
- Apache: Opens port 80, for standard HTTP traffic.
- Apache Full: Opens both port 80 and port 443, for HTTP and secure HTTPS traffic.
- Apache Secure: Opens only port 443, for HTTPS-only traffic.
-
Allow Apache Traffic: For a basic setup, especially before configuring SSL/TLS, allowing traffic on port 80 is sufficient. Choose the
Apache
profile:sudo ufw allow 'Apache'
If you plan to configure SSL later and want to allow both HTTP and HTTPS traffic immediately, you could use
Apache Full
. However, for now, ‘Apache’ profile is adequate. -
Verify Firewall Changes: Check the status of your firewall to confirm the new rule:
sudo ufw status
The output should indicate that the ‘Apache’ profile is now allowed:
Output Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
This confirms that your firewall is configured to allow HTTP traffic to your Apache server.
Step 3 — Checking Your Apache Web Server
After installation and firewall configuration, it’s time to verify if your Apache server setup on Ubuntu is successful and running correctly.
-
Systemd Status Check: Ubuntu 20.04 uses
systemd
to manage services. Check Apache’s status with:sudo systemctl status apache2
A successful output will show the service as
active (running)
:Output ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since ... ...
This indicates Apache is up and running.
-
Access Apache Default Page: The most definitive test is to access the default Apache landing page through a web browser. You’ll need your server’s IP address. Find this by:
hostname -I
or using an external service:
curl -4 icanhazip.com
Once you have your server’s IP address, enter it into your browser:
http://your_server_ip
You should see the default Ubuntu Apache page:
Default Apache landing page on Ubuntu 20.04 confirming successful installation.
This page confirms that your Apache server is correctly installed and accessible.
Step 4 — Managing the Apache Process
Knowing how to manage the Apache service is essential for day-to-day server administration. systemctl
commands are used to control Apache.
-
Stop Apache: To stop your Apache server, use:
sudo systemctl stop apache2
-
Start Apache: To start it again when stopped:
sudo systemctl start apache2
-
Restart Apache: To restart the server (useful after configuration changes):
sudo systemctl restart apache2
-
Reload Apache: For configuration changes that don’t require dropping existing connections, a reload is more efficient:
sudo systemctl reload apache2
-
Disable Auto-start on Boot: To prevent Apache from starting automatically at boot:
sudo systemctl disable apache2
-
Enable Auto-start on Boot: To re-enable automatic start-up at boot:
sudo systemctl enable apache2
These commands are fundamental for managing your Apache server’s lifecycle.
Step 5 — Setting Up Virtual Hosts for Multiple Websites
Virtual hosts are a critical aspect of apache server setup ubuntu, allowing you to host multiple websites on a single server. Apache uses virtual hosts to differentiate between domains and serve the correct content based on the domain name requested.
-
Create Directory for Your Domain: For each website you host, it’s best practice to create a separate directory. For a domain
your_domain
, create:sudo mkdir /var/www/your_domain
-
Set Directory Ownership: Assign ownership of this directory to your user account:
sudo chown -R $USER:$USER /var/www/your_domain
-
Set Permissions (if needed): Ensure permissions are set correctly. For web roots,
755
is generally appropriate:sudo chmod -R 755 /var/www/your_domain
-
Create a Sample
index.html
Page: Create a basic HTML file for testing:sudo nano /var/www/your_domain/index.html
Add the following content:
<!DOCTYPE html> <html> <head> <title>Welcome to Your_domain!</title> </head> <body> <h1>Success! The your_domain virtual host is working!</h1> </body> </html>
Save and close the file.
-
Create Virtual Host Configuration File: Create a new configuration file for your domain in
/etc/apache2/sites-available/
:sudo nano /etc/apache2/sites-available/your_domain.conf
Paste the following configuration, adjusting
ServerName
,ServerAlias
, andDocumentRoot
for your domain:<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName your_domain ServerAlias www.your_domain DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
DocumentRoot
: Specifies the directory where website files are stored.ServerName
: Your domain name.ServerAlias
: Any additional names for your site (likewww
).ServerAdmin
: Administrator email.
Save and close the file.
-
Enable the Virtual Host: Use
a2ensite
to enable your new virtual host:sudo a2ensite your_domain.conf
-
Disable Default Site: If you only want to serve your new domain, disable the default site:
sudo a2dissite 000-default.conf
-
Test Configuration: Always test for configuration errors before restarting Apache:
sudo apache2ctl configtest
Look for
Syntax OK
in the output. -
Restart Apache: Apply the changes by restarting Apache:
sudo systemctl restart apache2
Now, accessing
your_domain
in a browser should display your testindex.html
page, confirming your virtual host setup is successful.
Step 6 – Understanding Important Apache Files and Directories
To effectively manage your apache server setup ubuntu, it’s crucial to be familiar with key files and directories.
Content Directories:
/var/www/html
: Default directory for web content. For single-site setups, files are often placed here./var/www/your_domain
: Example of a virtual host directory, storing website files foryour_domain
.
Server Configuration Directories:
/etc/apache2
: Main Apache configuration directory./etc/apache2/apache2.conf
: Main configuration file, sets global Apache settings./etc/apache2/ports.conf
: Defines ports Apache listens on (default is 80, 443 for SSL)./etc/apache2/sites-available/
: Stores virtual host configuration files./etc/apache2/sites-enabled/
: Enabled virtual host configurations (symlinks to files insites-available
). Usea2ensite
anda2dissite
to manage./etc/apache2/conf-available/
,/etc/apache2/conf-enabled/
: For configuration snippets not specific to virtual hosts. Usea2enconf
anda2disconf
./etc/apache2/mods-available/
,/etc/apache2/mods-enabled/
: Manages Apache modules. Usea2enmod
anda2dismod
.
Server Log Directories:
/var/log/apache2/access.log
: Logs every request to your server./var/log/apache2/error.log
: Records errors encountered by Apache.
Understanding these files and directories is essential for troubleshooting, configuration adjustments, and overall Apache server management.
Conclusion
Congratulations! You’ve successfully completed the apache server setup ubuntu. You now have a running Apache web server on Ubuntu 20.04, capable of hosting websites. You’ve learned how to install Apache, configure your firewall, manage the Apache service, and set up virtual hosts for hosting multiple domains.
From here, you can explore further configurations, such as setting up SSL/TLS for secure HTTPS connections, optimizing Apache performance, and deploying web applications. Apache’s extensive features and Ubuntu’s user-friendly environment make a powerful combination for web hosting.
To expand your server capabilities, consider exploring how to set up a complete LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu for dynamic web applications.