This guide covers the setup for both Ubuntu and Debian systems. The commands are the same for both distributions unless specified otherwise.
To set up an Apache2 web server on a Linux system, follow these steps:
Make sure your package lists are up-to-date.
sudo apt update
You can install Apache2 using the following command:
sudo apt install apache2 -y
Apache2 registers itself with ufw
(Uncomplicated Firewall) upon installation. To allow traffic, enable the necessary firewall rules:
sudo ufw allow 'Apache'
Verify the status of the firewall:
sudo ufw status
You can start, stop, or restart the Apache2 service with the following commands:
Start Apache2:
sudo systemctl start apache2
Stop Apache2:
sudo systemctl stop apache2
Restart Apache2:
sudo systemctl restart apache2
Enable Apache2 to start at boot:
sudo systemctl enable apache2
Check the status of the service:
sudo systemctl status apache2
Once Apache2 is running, you can test if it is working by visiting your server’s IP address in a browser. For example:
http://your-server-ip
You should see the Apache2 default welcome page.
To configure Apache, you can modify files in the /etc/apache2/
directory.
The main configuration file is:
/etc/apache2/apache2.conf
Virtual hosts are stored in:
/etc/apache2/sites-available/
To edit or create a new virtual host, copy the default configuration:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your-site.conf
Then, edit your-site.conf
with a text editor:
sudo nano /etc/apache2/sites-available/your-site.conf
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/your-site
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the virtual host:
sudo a2ensite your-site.conf
Disable the default site if needed:
sudo a2dissite 000-default.conf
Reload Apache to apply changes:
sudo systemctl reload apache2
Ensure the correct permissions for your web root directory:
sudo chown -R www-data:www-data /var/www/your-site
sudo chmod -R 755 /var/www/your-site
You may need to enable additional modules for Apache. For example, to enable mod_rewrite:
sudo a2enmod rewrite
sudo systemctl restart apache2