close
close
apache virtual host centos 7 mas cerbot

apache virtual host centos 7 mas cerbot

3 min read 10-09-2024
apache virtual host centos 7 mas cerbot

If you're looking to host multiple websites on a single server using Apache on CentOS 7, setting up Virtual Hosts is the way to go. Additionally, securing those websites with SSL certificates from Let’s Encrypt using Certbot can be a great way to ensure security and trust. In this article, we will guide you through the process of configuring Apache virtual hosts on CentOS 7 and securing them using Certbot.

What is a Virtual Host?

A Virtual Host allows you to run multiple websites (domains) on a single server. Each website will have its own configuration settings and can operate independently of one another. This is particularly useful for shared hosting environments.

Setting Up Apache on CentOS 7

Before configuring virtual hosts, ensure that Apache is installed and running on your CentOS 7 server.

sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd

To verify that Apache is running, you can visit your server's IP address in a web browser. You should see the default Apache welcome page.

Configuring Apache Virtual Hosts

  1. Create Directory Structure

    For this example, let's say we want to set up two domains: example1.com and example2.com. Start by creating the document root directories for these domains.

    sudo mkdir -p /var/www/example1.com/public_html
    sudo mkdir -p /var/www/example2.com/public_html
    

    Next, create a sample index.html file for each domain.

    echo "<h1>Welcome to Example1.com!</h1>" | sudo tee /var/www/example1.com/public_html/index.html
    echo "<h1>Welcome to Example2.com!</h1>" | sudo tee /var/www/example2.com/public_html/index.html
    
  2. Create Virtual Host Configuration Files

    Now, you need to create virtual host files for each domain in the /etc/httpd/conf.d/ directory.

    For example1.com:

    sudo nano /etc/httpd/conf.d/example1.com.conf
    

    Add the following content:

    <VirtualHost *:80>
        ServerName example1.com
        ServerAlias www.example1.com
        DocumentRoot /var/www/example1.com/public_html
        ErrorLog /var/log/httpd/example1.com-error.log
        CustomLog /var/log/httpd/example1.com-access.log combined
    </VirtualHost>
    

    For example2.com, repeat the process:

    sudo nano /etc/httpd/conf.d/example2.com.conf
    

    Add:

    <VirtualHost *:80>
        ServerName example2.com
        ServerAlias www.example2.com
        DocumentRoot /var/www/example2.com/public_html
        ErrorLog /var/log/httpd/example2.com-error.log
        CustomLog /var/log/httpd/example2.com-access.log combined
    </VirtualHost>
    
  3. Restart Apache

    After creating the virtual host files, restart Apache to apply the changes.

    sudo systemctl restart httpd
    

Installing Certbot and Obtaining SSL Certificates

Certbot is a powerful tool that automates the process of obtaining and renewing SSL certificates from Let’s Encrypt.

  1. Install Certbot

    You may need to enable the EPEL repository first.

    sudo yum install epel-release
    

    Then, install Certbot:

    sudo yum install certbot python2-certbot-apache
    
  2. Obtain SSL Certificates

    To obtain SSL certificates for your domains, run the following command. Certbot will automatically configure SSL in your virtual host files.

    sudo certbot --apache
    

    Follow the on-screen prompts to complete the setup. Certbot will automatically update your virtual host files to redirect HTTP traffic to HTTPS.

  3. Renewing SSL Certificates

    Let’s Encrypt certificates expire every 90 days. To automatically renew them, you can set up a cron job. Check if the renewal process is working with the following command:

    sudo certbot renew --dry-run
    

    If it works without any errors, you can add this command to your crontab:

    echo "0 0 * * * root certbot renew --quiet" | sudo tee -a /etc/crontab
    

Additional Tips

  • Firewall Configuration: Ensure your firewall allows HTTP (port 80) and HTTPS (port 443). You can use the following commands to open these ports:

    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
    
  • DNS Configuration: Ensure that your DNS records are properly set up for both domains, pointing to your server's IP address.

Conclusion

Setting up Apache Virtual Hosts on CentOS 7 and securing them with SSL certificates using Certbot is a straightforward process that enhances your server's capabilities and security. With this setup, you can efficiently host multiple sites on a single server and keep them secured.

Feel free to explore more about Apache configurations and SSL setup by visiting their official documentation. Happy hosting!


References

By following these guidelines, you can ensure a secure and efficient hosting environment for your websites.

Related Posts


Popular Posts