Skip to content

Instantly share code, notes, and snippets.

@jperocho
Last active February 1, 2023 16:01
Show Gist options
  • Save jperocho/e42b510d3236fcb7de5b55657a386243 to your computer and use it in GitHub Desktop.
Save jperocho/e42b510d3236fcb7de5b55657a386243 to your computer and use it in GitHub Desktop.
Rocky Linux: Install latest Wordpress Lemp with PHP 8.0
#!/bin/bash
# This script is for Rocky Linux or any same distribution
# Caution: This script is designed for a fresh installation of Rocky Linux. If you already have a WordPress installation on your server, it is not suitable for your use case and it will overwrite your existing installation. Use it at your own risk, as it may cause unintended consequences.
#
# How to run:
# curl -o setup-wp-lemp.sh https://gist.githubusercontent.com/jperocho/e42b510d3236fcb7de5b55657a386243/raw/setup-wp-lemp.sh && chmod +x setup-wp-lemp.sh && ./setup-wp-lemp.sh
# Install the yum-plugin-fastestmirror package
sudo yum install yum-plugin-fastestmirror -y
# Enable the fastestmirror plugin
sudo sed -i 's/enabled=0/enabled=1/g' /etc/yum/pluginconf.d/fastestmirror.conf
# Update the package index
sudo yum makecache fast
# Install language pack
sudo yum install langpacks-en glibc-all-langpacks -y
# Set your locale
sudo localectl set-locale LANG=en_PH.UTF-8
# Update the environment variables
echo "export LANG=en_PH.UTF-8" >> ~/.bashrc
source ~/.bashrc
# Prompt the user for the hostname
echo -n "Enter hostname: "
read hostname
# Add www version of the hostname
www_hostname="www.$hostname"
# Update package index
sudo yum update -y
# Install Nginx
sudo yum install nginx -y
# Start Nginx and enable it to start on boot
sudo systemctl start nginx
sudo systemctl enable nginx
# Install MySQL
sudo yum install mariadb-server mariadb -y
# Start MySQL and enable it to start on boot
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Install PHP 8 and necessary modules
sudo yum install centos-release-stream
sudo yum module reset php
sudo yum module install php:8.0
sudo yum install php-fpm php-mysqlnd -y
# Configure PHP-FPM to use socket instead of port
sed -i 's/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm\/php-fpm.sock/g' /etc/php-fpm.d/www.conf
# Change ownership of the /usr/share/nginx/html folder to nginx user and group
sudo chown -R nginx:nginx /usr/share/nginx/html/
# Set proper permissions for WordPress files and folders
sudo chmod -R 755 /usr/share/nginx/html/
sudo find /usr/share/nginx/html/ -type d -exec chmod 755 {} \;
sudo find /usr/share/nginx/html/ -type f -exec chmod 644 {} \;
sudo chmod 660 /usr/share/nginx/html/wp-config.php
# Change the user of the PHP-FPM process to nginx
sudo sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sudo sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
# Start PHP-FPM and enable it to start on boot
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
# Backup the original Nginx configuration file
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo cp /etc/nginx/default.d/php.conf /etc/nginx/default.d/php.conf.bak
# Remove all location blocks from the Nginx configuration file
sudo sed -i '/^error_page/d' /etc/nginx/nginx.conf
sudo sed -i "/location $location/,/^\s*}/d" /etc/nginx/nginx.conf
sudo sed -i "/location $location/,/^\s*}/d" /etc/nginx/default.d/php.conf
# Configure Nginx to use PHP-FPM
sudo sed -i 's/index index.html index.htm;/index index.php index.html index.htm;/g' /etc/nginx/nginx.conf
sudo echo 'location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
}' >> /etc/nginx/default.d/php.conf
# Set hostname in Nginx configuration
sudo sed -i "s/server_name _;/server_name $hostname $www_hostname;/g" /etc/nginx/nginx.conf
# Update /etc/hosts file
echo "127.0.0.1 $hostname $www_hostname" | sudo tee -a /etc/hosts
# Restart Nginx
sudo systemctl restart nginx
# Create a new database and user for WordPress
sudo mysql -e "CREATE DATABASE wordpress;"
# Generate a random password for the WordPress database user
password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
sudo mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost' IDENTIFIED BY '$password';"
sudo mysql -e "FLUSH PRIVILEGES;"
# Download and extract WordPress
sudo yum install wget -y
wget https://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz -C /usr/share/nginx/html/
sudo mv /usr/share/nginx/html/wordpress/* /usr/share/nginx/html/
sudo rm -rf /usr/share/nginx/html/wordpress/
sudo rm -f latest.tar.gz
# Set ownership and permissions
sudo chown -R nginx:nginx /usr/share/nginx/html/
sudo chmod -R 755 /usr/share/nginx/html/
# Create a wp-config.php file
sudo cp /usr/share/nginx/html/wp-config-sample.php /usr/share/nginx/html/wp-config.php
# Update the wp-config.php file with the correct database information
sudo sed -i "s/database_name_here/wordpress/g" /usr/share/nginx/html/wp-config.php
sudo sed -i "s/username_here/wpuser/g" /usr/share/nginx/html/wp-config.php
sudo sed -i "s/password_here/$password/g" /usr/share/nginx/html/wp-config.php
# Print the randomly generated password to the console
echo "WordPress database password: $password"
# Retrieve the WordPress security salts
salts=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
# Add the security salts to the wp-config.php file
echo "$salts" >> /usr/share/nginx/html/wp-config.php
# Print the security salts to the console
echo "WordPress security salts: $salts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment