Apache Web Server Installation And Configuration In Linux
Why Apache Web Server?
Apache is the most widely used web server software on the internet. It is open-source, free to use, and highly customizable. Apache offers a stable and secure platform for hosting websites and applications, making it the preferred choice for many developers and system administrators.
Installation
Step 1: Update System Packages
Before installing Apache, it is recommended to update the system packages using the following command:
sudo apt update
Step 2: Install Apache
To install Apache on a Linux system, use the package manager. For Ubuntu, use the following command:
sudo apt install apache2
Configuration
Step 1: Firewall Configuration
By default, Apache listens on port 80. To allow incoming traffic, open port 80 on your firewall using the following command:
sudo ufw allow 80
Step 2: Virtual Host Configuration
If you plan to host multiple websites or applications on your server, you can configure virtual hosts. Create a new configuration file for each website in the ‘/etc/apache2/sites-available/’ directory. For example:
sudo nano /etc/apache2/sites-available/example.com.conf
Step 3: Enable the Virtual Host
After creating the virtual host configuration file, enable it using the following command:
sudo a2ensite example.com.conf
Step 4: Disable the Default Virtual Host
By default, Apache comes with a default virtual host configuration. Disable it to avoid conflicts with your custom virtual hosts:
sudo a2dissite 000-default.conf
Step 5: Restart Apache
After making any configuration changes, restart Apache for the changes to take effect:
sudo systemctl restart apache2
Directory Structure
Apache follows a specific directory structure to store website files and configurations:
/var/www/html
: The default document root directory where website files are stored./etc/apache2
: The main configuration directory for Apache./etc/apache2/sites-available
: Directory containing virtual host configuration files./etc/apache2/sites-enabled
: Directory containing symbolic links to enabled virtual host configurations.
Managing Apache Services
Start Apache
To start the Apache service, use the following command:
sudo systemctl start apache2
Stop Apache
To stop the Apache service, use the following command:
sudo systemctl stop apache2
Restart Apache
To restart the Apache service, use the following command:
sudo systemctl restart apache2
Enable Apache at Boot
To automatically start Apache on system boot, use the following command:
sudo systemctl enable apache2
Disable Apache at Boot
To disable the automatic start of Apache on system boot, use the following command:
sudo systemctl disable apache2
Conclusion
Installing and configuring Apache web server in Linux is a straightforward process. By following the steps outlined in this article, you can set up a reliable and secure web hosting environment. Apache’s flexibility and extensive documentation make it an excellent choice for both beginners and experienced users.