Configuring Your CentOS VPS with Apache for Website Hosting
This tutorial provides a step-by-step guide to configuring Apache on your CentOS VPS for hosting websites, covering installation, configuration, and basic security measures.
centosapache
Large
VPS
3
vCPU
12 GB
Memory
120 GB
NVMe Disk
6144 GB
Traffic
29.90€
/month
* Up to 6 vCPU, 36GB RAM, 360GB NVMe Disk Space and 1Gbit/s Network Speed
#### Prerequisites
Before you begin, ensure you have:
- A CentOS VPS (This guide is tested with CentOS 8)
- A user account with sudo privileges
- Basic familiarity with the command line
- A domain name (optional but recommended)
#### Step 1: Update Your System
Start by updating all packages to their latest versions:
```bash
sudo dnf update -y
```
#### Step 2: Install Apache
Install the Apache web server package:
```bash
sudo dnf install httpd -y
```
Start and enable Apache to run on system boot:
```bash
sudo systemctl start httpd
sudo systemctl enable httpd
```
#### Step 3: Adjust Firewall Settings
Allow HTTP and HTTPS traffic through the firewall:
```bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```
#### Step 4: Verify Apache Installation
Open a web browser and enter your server’s IP address. You should see the Apache test page confirming that Apache is running.
#### Step 5: Configure Apache
Create a directory for your website files:
```bash
sudo mkdir -p /var/www/your_domain
```
Set the correct permissions:
```bash
sudo chown -R $USER:$USER /var/www/your_domain
sudo chmod -R 755 /var/www/your_domain
```
Create a sample HTML file to test your configuration:
```bash
echo "
Welcome to Your Domain
" > /var/www/your_domain/index.html
```
Create a new virtual host configuration file for your domain:
```bash
sudo nano /etc/httpd/conf.d/your_domain.conf
```
Add the following content to the file:
```plaintext
ServerAdmin admin@your_domain
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog /var/log/httpd/your_domain-error.log
CustomLog /var/log/httpd/your_domain-access.log combined
```
Replace `your_domain` with your actual domain name and `admin@your_domain` with your email address.
Save and exit the file, then restart Apache to apply the changes:
```bash
sudo systemctl restart httpd
```
#### Step 6: Test Your Configuration
Open a web browser and enter your domain name. You should see the sample HTML file you created earlier, confirming that Apache is serving your website correctly.
#### Step 7: Set Up SSL with Let's Encrypt (Optional)
For secure HTTPS connections, you can use Let's Encrypt to obtain a free SSL certificate.
Install Certbot:
```bash
sudo dnf install certbot python3-certbot-apache -y
```
Obtain and install the SSL certificate:
```bash
sudo certbot --apache
```
Follow the prompts to complete the installation. Certbot will automatically configure Apache to use the SSL certificate.
#### Step 8: Configure Automatic Certificate Renewal
Let's Encrypt certificates expire every 90 days. Set up a cron job to renew the certificates automatically:
Open the crontab editor:
```bash
sudo crontab -e
```
Add the following line to renew the certificates daily:
```plaintext
0 0 * * * /usr/bin/certbot renew --quiet
```
Save and exit the crontab editor.
#### Step 9: Basic Security Measures
**Disable Directory Listing:**
Open the Apache configuration file:
```bash
sudo nano /etc/httpd/conf/httpd.conf
```
Find and change the following line:
```plaintext
Options Indexes FollowSymLinks
```
To:
```plaintext
Options FollowSymLinks
```
Save and exit the file, then restart Apache:
```bash
sudo systemctl restart httpd
```
**Hide Apache Version and OS Information:**
Edit the Apache configuration file:
```bash
sudo nano /etc/httpd/conf/httpd.conf
```
Add the following lines at the end of the file:
```plaintext
ServerTokens Prod
ServerSignature Off
```
Save and exit the file, then restart Apache:
```bash
sudo systemctl restart httpd
```
#### Conclusion
You have successfully configured your CentOS VPS with Apache for website hosting. Your server is now ready to host your website, and you have taken basic steps to secure it. Regularly monitor and update your server to ensure optimal performance and security.
**Additional Resources:**
- **Apache Documentation:** [https://httpd.apache.org/docs/](https://httpd.apache.org/docs/)
- **Let's Encrypt Documentation:** [https://letsencrypt.org/docs/](https://letsencrypt.org/docs/)
- **CentOS Documentation:** [https://www.centos.org/docs/](https://www.centos.org/docs/)