Sunday, December 22, 2024

Automate the Setup of LAMP, WordPress, and Let’s Encrypt on CentOS 9 Stream

Share

Here’s a sample bash script that automates the setup of LAMP (Linux, Apache, MySQL, PHP), WordPress, and installs a Let’s Encrypt SSL certificate on CentOS 9 Stream:

Code to Automate the Setup of LAMP

#!/bin/bash

# Prompt for domain name
read -p "Enter your domain name (e.g., example.com): " domain

# Prompt for www. variant
read -p "Enter the www. variant of your domain (e.g., www.example.com): " www_domain

# Update system packages
sudo dnf update -y

# Install Apache, MySQL/MariaDB, PHP, and other required packages
sudo dnf install -y httpd mariadb-server php php-mysqlnd php-json php-gd php-mbstring

# Start and enable Apache and MariaDB services
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl start mariadb
sudo systemctl enable mariadb

# Secure MariaDB installation
sudo mysql_secure_installation

# Create a new MariaDB database and user for WordPress
sudo mysql -u root -p <<MYSQL_SCRIPT
CREATE DATABASE wordpress;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
MYSQL_SCRIPT

# Install Certbot for Let's Encrypt
sudo dnf install -y certbot python3-certbot-apache

# Obtain and install SSL certificate using Certbot
sudo certbot --apache -d "$domain" -d "$www_domain"

# Download and extract WordPress
cd /var/www/html
sudo wget -c http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* .
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html

# Configure virtual host for the domain
sudo cp /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/"$domain".conf
sudo sed -i "s:/var/www/html:/var/www/html/:" /etc/httpd/conf.d/"$domain".conf
sudo sed -i "s:ServerName.*:ServerName $domain:" /etc/httpd/conf.d/"$domain".conf
sudo sed -i "s:ServerAlias.*:ServerAlias $www_domain:" /etc/httpd/conf.d/"$domain".conf

# Restart Apache to apply changes
sudo systemctl restart httpd

# Optimize PHP and Apache
sudo sed -i 's/;date.timezone =.*/date.timezone = UTC/' /etc/php.ini
sudo sed -i 's/memory_limit = 128M/memory_limit = 256M/' /etc/php.ini
sudo sed -i 's/max_execution_time = 30/max_execution_time = 60/' /etc/php.ini
sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/' /etc/php.ini
sudo sed -i 's/post_max_size = 8M/post_max_size = 64M/' /etc/php.ini

# Display setup completion message
echo "LAMP, WordPress, Let's Encrypt, virtual host, and optimization setup is complete!"

Save the above script in a file (e.g., setup.sh), make it executable using chmod +x setup.sh, and then execute it with ./setup.sh. The script will guide you through the setup process, prompting you to enter your domain name and perform the necessary installations and configurations.

Please note that running a script like this should be done with caution, as it will make changes to your system configuration. Ensure you have backups and review the script to make any necessary modifications to fit your specific requirements.

Related Articles

Read more

Local News