Create the EC2 instance with AWS and launch the instance by selecting the Ubuntu OS. Once the instance is ready state enable SSH on the instance to access via your command line interface and execute the script mentioned below.
create new file with the name of automated_wordpress_apache_ssl_script.sh
Enter the following command in sequence to execute at once:
!/bin/bash
Define parameters
DB_NAME=”wordpress”
DB_USER=”Custome_Name”
DB_PASSWORD=”Custome_Password”
WP_PATH=”/var/www/html/wordpress”
WP_URL=”http://yourdomain.com/wordpress”
Update system
$ sudo apt update
$ sudo apt upgrade -y
Install required packages
sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql unzip
Secure MySQL installation
sudo mysql_secure_installation
Install Certbot for Let’s Encrypt
sudo apt install -y certbot python3-certbot-apache
Create the WordPress database and user
sudo mysql -e “CREATE DATABASE $DB_NAME;”
sudo mysql -e “CREATE USER ‘$DB_USER’@’localhost’ IDENTIFIED BY ‘$DB_PASSWORD’;”
sudo mysql -e “GRANT ALL PRIVILEGES ON $DB_NAME.* TO ‘$DB_USER’@’localhost’;”
sudo mysql -e “FLUSH PRIVILEGES;”
Download and set up WordPress
sudo rm -rf /var/www/html/*
sudo wget -qO- https://wordpress.org/latest.tar.gz | sudo tar xvz -C /var/www/html/
sudo mv /var/www/html/wordpress/* /var/www/html/
sudo rm -rf /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
Configure WordPress
sudo cp $WP_PATH/wp-config-sample.php $WP_PATH/wp-config.php
sudo sed -i “s/database_name_here/$DB_NAME/g” $WP_PATH/wp-config.php
sudo sed -i “s/username_here/$DB_USER/g” $WP_PATH/wp-config.php
sudo sed -i “s/password_here/$DB_PASSWORD/g” $WP_PATH/wp-config.php
Configure Apache
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/wp.techmahato.com.conf
sudo sed -i “s|DocumentRoot /var/www/html|DocumentRoot $WP_PATH|” /etc/apache2/sites-available/wp.techmahato.com.conf
sudo sed -i “s|||” /etc/apache2/sites-available/wp.techmahato.com.conf
Enable SSL with Certbot
sudo certbot –apache -d yourdomain.com
Schedule auto-renewal with a cron job
sudo crontab -l | { cat; echo “0 0 * * 0 certbot renew –quiet –post-hook ‘systemctl reload apache2′”; } | sudo crontab –
Restart Apache
sudo systemctl restart apache2
echo “WordPress with Let’s Encrypt SSL and auto-renewal has been successfully set up!”
echo “Visit $WP_URL to access your secure WordPress site.”
Finally once the above script is ready save the script and run the following command to begin the setup:
Run the script to continue to the setup automated_wordpress_apache_ssl_script.sh