Installing the Apache web server on CentOS 7 is an easy process that will get your web server up and running quickly. Apache is one of the most popular and widely used web servers in the world due to its flexibility, power, and ease of configuration. This guide will walk you through each step of the installation process, from updating your system to configuring Apache.
Steps to Install Apache Web Server on CentOS 7
Step 1: Update Your System
Before installing any new software, it's important to ensure your system is up to date. Open your terminal and execute the following command to update all your packages:
sudo yum update -y
This command updates all the currently installed packages to their latest versions.
Step 2: Install Apache
Once your system is updated, you can proceed with the installation of the Apache web server. The package name for Apache in CentOS is httpd. Install it using the following command:
sudo yum install httpd -y
This command installs the Apache web server along with any dependencies.
Step 3: Start and Enable Apache
After installing Apache, you need to start the service and ensure it starts automatically at boot. Use the following commands to start Apache and enable it:
sudo systemctl start httpd
sudo systemctl enable httpd
The start command starts the Apache service, and the enable command ensures that Apache starts automatically whenever the system is booted.
Step 4: Adjust Firewall Settings
To allow web traffic to reach your Apache web server, you need to adjust your firewall settings. Use the following commands to allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
These commands open the necessary ports for HTTP (port 80) and HTTPS (port 443) traffic.
Step 5: Verify Apache Installation
To verify that Apache is installed and running correctly, open a web browser and enter your server's IP address. You should see the Apache test page, which means the installation was successful. You can find your server's IP address using the following command:
hostname -I
Enter the IP address displayed in the terminal into your web browser’s address bar. If Apache is working correctly, you will see the Apache welcome page.
Step 6: Configure Apache
The default configuration files for Apache are located in the /etc/httpd directory. The main configuration file is /etc/httpd/conf/httpd.conf. You can edit this file to customize your Apache settings. Open the configuration file using a text editor like nano:
sudo nano /etc/httpd/conf/httpd.conf
Make any necessary changes, then save and close the file. After making changes to the
configuration file, you need to restart Apache to apply the changes:
sudo systemctl restart httpd
Step 7: Create a Website Directory
By default, Apache serves files from the /var/www/html directory. You can create your own directory for your website and point Apache to serve files from there. For example, create a directory for your website:
sudo mkdir /var/www/mywebsite
Set the correct permissions for the directory:
sudo chown -R $USER:$USER /var/www/mywebsite
Create an HTML file to test your website:
echo "<html><body><h1>Hello, World!</h1></body></html>" > /var/www/mywebsite/index.html
Step 8: Configure Virtual Hosts
Virtual hosts allow you to run multiple websites on a single server. To set up a virtual host, create a new configuration file in the /etc/httpd/conf.d directory:
sudo nano /etc/httpd/conf.d/mywebsite.conf
Add the following content to the file:
<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
DocumentRoot /var/www/mywebsite
ErrorLog /var/log/httpd/mywebsite-error.log
CustomLog /var/log/httpd/mywebsite-access.log combined
</VirtualHost>
Save and close the file. Restart Apache to apply the changes:
sudo systemctl restart httpd
Step 9: Test Your Configuration
To test your virtual host configuration, you need to update your local hosts file. On your local machine, add an entry for your website:
sudo nano /etc/hosts
Add the following line, replacing your_server_ip with your server's IP address:
your_server_ip mywebsite.com mywebsite.com
Save and close the file. Open a web browser and enter mywebsite.com in the address bar. You should see your "Hello, World!" message, indicating that the virtual host is working correctly.
Conclusion
You have successfully installed and configured Apache on CentOS 7. Your web server is now ready to serve websites. By following these steps, you can ensure a smooth and secure installation of Apache, allowing you to host and manage your web content efficiently. Whether you're setting up a personal blog, a business site, or a complex web application, Apache on CentOS 7 provides a reliable and robust platform to meet your needs.