To install Apache on Ubuntu, open a terminal and run sudo apt update followed by sudo apt install apache2. This installs the Apache web server, starts it automatically, and configures it to launch at boot. Below is a complete walkthrough covering installation, configuration, virtual hosts, firewall settings, and common troubleshooting.
What Is Apache and Why Use It?
Apache HTTP Server (often called just Apache) is the most widely used open-source web server software. It serves web pages over HTTP and HTTPS, handling requests from browsers and delivering files like HTML, CSS, JavaScript, images, and application responses. Its popularity comes from being free, highly configurable, and supported on virtually every operating system, including Ubuntu.
Ubuntu’s default repositories include Apache, so installation is straightforward and secure because you get security patches through the standard apt update process. Apache is a solid choice for hosting personal websites, development environments, and production servers, especially when you need modules for URL rewriting, SSL, or proxying.
Prerequisites
Before starting, ensure you have:
- Ubuntu 20.04, 22.04, 24.04, or a newer LTS version (the steps are the same for nearly all releases).
- A user account with
sudoprivileges (the default Ubuntu user typically has this). - An internet connection to download packages.
- Optional: a domain name pointed to your server’s IP address if you plan to host a public site.
Step-by-Step Installation
- Update package lists: Run
sudo apt updateto refresh the list of available packages. This ensures you get the latest version of Apache and its dependencies. - Install Apache: Run
sudo apt install apache2. When prompted, pressYand Enter. Ubuntu will download and install Apache along with required libraries. - Verify installation: After installation, Apache starts automatically. Check its status with
sudo systemctl status apache2. You should seeactive (running). - Test the web server: Open a browser and go to
http://your_server_iporhttp://localhostif you are on the server itself. You should see the default Apache welcome page, which confirms everything works.
If the status shows inactive or failed, start it manually with sudo systemctl start apache2 and check for errors in the logs at /var/log/apache2/error.log.
Configuring the Firewall (UFW)
Ubuntu often ships with the Uncomplicated Firewall (UFW) disabled. If you enable it, you must allow web traffic:
- Allow HTTP (port 80):
sudo ufw allow 'Apache' - Allow HTTPS (port 443):
sudo ufw allow 'Apache Secure' - Enable UFW if not already:
sudo ufw enable(be careful not to lock yourself out – allow SSH first:sudo ufw allow 'OpenSSH')
Check the status with sudo ufw status. You should see rules for Apache and Apache Secure if you added both.
Managing the Apache Service
Apache runs as a systemd service. Common commands include:
sudo systemctl stop apache2– stops Apache.sudo systemctl start apache2– starts Apache.sudo systemctl restart apache2– restarts (stops then starts) – good after config changes.sudo systemctl reload apache2– reloads configuration without dropping connections – best for minor changes.sudo systemctl enable apache2– enables Apache to start at boot (usually automatic, but this ensures it).sudo systemctl disable apache2– disables auto-start at boot.
After editing any configuration file, run sudo apachectl configtest to check for syntax errors before reloading or restarting. This prevents downtime from typos.
Setting Up Virtual Hosts
Virtual hosts let you host multiple websites on one server. Each site has its own document root (folder) and configuration file. The default site is 000-default.conf. To create a new site:
- Create a directory for your site:
sudo mkdir -p /var/www/example.com/public_html - Set ownership (replace
your_userwith your username):sudo chown -R $USER:$USER /var/www/example.com/public_html - Create a sample index file:
echo 'Hello World' | tee /var/www/example.com/public_html/index.html - Copy the default config:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf - Edit the new config:
sudo nano /etc/apache2/sites-available/example.com.confand changeServerAdminandDocumentRootto point to your folder. Add aServerName example.comline. - Enable the site:
sudo a2ensite example.com.conf - Disable the default site (optional):
sudo a2dissite 000-default.conf - Reload Apache:
sudo systemctl reload apache2
Now your domain (or IP if you didn’t set a domain) will serve the new content.
Common Problems and Fixes
Here are frequent issues and how to resolve them:
- Port 80 already in use: If another web server (like nginx) is running, stop it with
sudo systemctl stop nginxor change Apache’s port in/etc/apache2/ports.conf. - Permission denied errors: Ensure the web server user (
www-data) can read your files. For a home directory, addwww-datato your user’s group:sudo usermod -a -G your_user www-data, then set folder permissions to750or755. - Configuration syntax errors: Run
sudo apachectl configtest– it will tell you the line number of the problem. - Firewall blocks access: If you can’t reach the server from another machine, check UFW status and ensure the Apache rules are active.
- DNS issues: If you set a ServerName, make sure your domain resolves to your server’s IP. Locally, you can edit
/etc/hostsfor testing.
Security Basics
After installation, consider these security steps:
- Enable HTTPS with a free Let’s Encrypt certificate:
sudo apt install certbot python3-certbot-apachethensudo certbot --apache. - Disable directory listing: edit the
/etc/apache2/apache2.conffile and setOptions -Indexesin the<Directory /var/www/>block. - Keep Apache updated:
sudo apt update && sudo apt upgraderegularly. - Remove unused modules:
sudo a2dismod autoindexis a common one to disable.
Frequently Asked Questions
How do I uninstall Apache completely?
Run sudo apt purge apache2 to remove Apache and its configuration files. To also remove dependencies that are no longer needed, use sudo apt autoremove. If you want to keep configuration files, use sudo apt remove apache2 instead.
Where are Apache’s configuration files?
The main configuration file is /etc/apache2/apache2.conf. Site-specific configurations live in /etc/apache2/sites-available/ and are enabled via symlinks in /etc/apache2/sites-enabled/. Modules are configured in /etc/apache2/mods-available/ and enabled in /etc/apache2/mods-enabled/.
How do I change Apache’s default port?
Edit /etc/apache2/ports.conf and change Listen 80 to your desired port (e.g., Listen 8080). Then update your virtual host file’s <VirtualHost *:80> to match. Restart Apache afterward. Remember to update firewall rules if you use UFW.
Why do I get a 403 Forbidden error?
This usually means Apache cannot read the files or directory. Check permissions: the directory should be executable (at least 755) and files readable (644). Also ensure the DocumentRoot path is correct and that there is an index.html or index.php file present.
Can I run Apache and Nginx together?
Yes, but they cannot both listen on the same port. You can run Apache on port 8080 and Nginx on 80, with Nginx acting as a reverse proxy. This is an advanced setup; for most users, choosing one is simpler.
How do I enable PHP support?
Install PHP and the Apache module: sudo apt install php libapache2-mod-php. Then restart Apache. To test, create a file info.php in your document root with <?php phpinfo(); ?> and view it in your browser.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Windows 10 Won’t Wake Up from Sleep
- iOS 11 Problems & Fixes: Common Issues Solved
- Fix Could Not Initialize Graphics System on Windows 10
- Best Microsoft Outlook Tips and Tricks
- Why Is WhatsApp Web Not Working? Fixes & Causes
- How to Turn On Data Roaming on iPhone 14