apache ,wordpress, ssl cert to it with www and non www donain
Setting up an Apache web server to host a WordPress website with SSL certificates for both www and non-www domains involves several steps. Here’s a detailed guide on how to do this:
Prerequisites:
- A domain name (e.g., example.com).
- Access to a Linux server with root or sudo privileges.
- Apache web server installed and running.
- WordPress installed and configured.
Step 1: Install Certbot and Obtain SSL Certificates
- Install Certbot on your server. The following example is for Ubuntu/Debian:
sudo apt-get update
sudo apt-get install certbot python3-certbot-apache
Step 2: Configure Apache VirtualHosts
- Create or edit your Apache VirtualHost configuration for the non-www version of your domain (
example.com
). You can find the Apache configuration file for your site in/etc/apache2/sites-available/
. Here’s an example VirtualHost configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
- Create a separate VirtualHost configuration for the www version of your domain (
www.example.com
):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/www.example.com-error.log
CustomLog ${APACHE_LOG_DIR}/www.example.com-access.log combined
</VirtualHost>
- Save the configuration files and exit the text editor.
Step 3: Enable the New VirtualHosts
- Enable the non-www and www VirtualHost configurations:
sudo a2ensite example.com.conf
sudo a2ensite www.example.com.conf
- Reload Apache to apply the changes:
sudo systemctl reload apache2
Step 4: Certbot to obtain SSL
- Use Certbot to obtain SSL certificates for both www and non-www versions of your domain. Replace
example.com
with your domain name:
sudo certbot --apache -d example.com -d www.example.com
Certbot will guide you through the certificate issuance process and configure your Apache VirtualHosts.
note: you must issue for both domains together as shown an above example , otherwise you will get error.
- Make sure your SSL configurations are present in the respective VirtualHost configurations for both domains. Certbot should have added these configurations when obtaining the SSL certificates.
Step 5: Test Your Configuration
- Test your Apache configuration for any syntax errors:
sudo apachectl configtest
- Ensure that your SSL certificates are correctly set up:
sudo certbot certificates
Step 6: Configure WordPress
- In your WordPress dashboard, go to “Settings” > “General.”
- Update the “WordPress Address (URL)” and “Site Address (URL)” to use the HTTPS protocol (https://example.com and https://www.example.com).
Step 7: Verify HTTPS
Visit your website using both www and non-www versions (e.g., https://example.com and https://www.example.com) to confirm that SSL is working correctly.
Your WordPress website should now be accessible over HTTPS with both www and non-www versions of your domain. Remember to renew your SSL certificates when necessary using Certbot’s automated renewal process.