Skip to content

Instantly share code, notes, and snippets.

@munabedan
Created January 26, 2024 19:36
Show Gist options
  • Save munabedan/fa2253e1098696c84db4c3b6e70204cd to your computer and use it in GitHub Desktop.
Save munabedan/fa2253e1098696c84db4c3b6e70204cd to your computer and use it in GitHub Desktop.
Install wordpress script
#!/bin/bash
# prompt user to run with sudo
if [ "$EUID" -ne 0 ]
then echo "Please run with sudo"
exit
fi
# Login to MySQL as root
# set user variable
user="root"
# set password variable
password="your_root_password_here"
# Check if the WordPress database exists
if mysql -u root -p$password -e "USE wordpress" >/dev/null 2>&1; then
# If the WordPress database exists, drop it
echo "Deleting the WordPress database..."
mysql -u root -p$password -e "DROP DATABASE wordpress"
fi
# Check if the WordPress database user exists
if mysql -u root -p$password -e "SELECT User FROM mysql.user WHERE User='wordpressuser'" | grep "wordpressuser" >/dev/null; then
# If the WordPress database user exists, delete it
echo "Deleting the WordPress database user..."
mysql -u root -p$password -e "DROP USER 'wordpressuser'@'%'"
fi
mysql -u $user -p$password -Bse "CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;CREATE USER 'wordpressuser'@'%' IDENTIFIED BY 'wordpress_pass';GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';FLUSH PRIVILEGES;"
# Create new database and user
# update
sudo apt update
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
sudo systemctl restart apache2
# override default apache2.conf
# Check if the wordpress.conf file already exists
if [ -f /etc/apache2/sites-available/wordpress.conf ]; then
echo "The wordpress.conf file already exists. Deleting it..."
sudo rm /etc/apache2/sites-available/wordpress.conf
fi
# Create the wordpress.conf file
sudo tee /etc/apache2/sites-available/wordpress.conf > /dev/null <<EOF
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/wordpress
<Directory /var/www/wordpress/>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
sudo a2enmod rewrite
# Enable the new virtual host configuration
sudo a2ensite wordpress.conf
# Test the Apache configuration
if ! sudo apache2ctl configtest; then
echo "Apache configuration test failed. Aborting."
exit 1
fi
# Restart Apache if the configuration test succeeds
sudo systemctl restart apache2
echo "Apache restarted successfully."
# Check if unzip is installed and install it if not
if ! command -v unzip &> /dev/null
then
echo "unzip command not found, installing..."
sudo apt-get update
sudo apt-get install -y unzip
fi
# Download and set up WordPress
echo "Downloading and setting up WordPress..."
cd /tmp
curl -O https://wordpress.org/latest.zip
unzip latest.zip
touch /tmp/wordpress/.htaccess
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
mkdir /tmp/wordpress/wp-content/upgrade
sudo cp -a /tmp/wordpress/. /var/www/wordpress
echo "WordPress has been downloaded and set up!"
# Adjust permissions
echo "Adjusting permissions..."
sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
echo "Permissions have been adjusted!"
# Set authentication unique keys and salts in wp-config.php
perl -i -pe '
BEGIN {
@chars = ("a" .. "z", "A" .. "Z", 0 .. 9);
push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|";
sub salt { join "", map $chars[ rand @chars ], 1 .. 64 }
}
s/put your unique phrase here/salt()/ge
' /var/www/wordpress/wp-config.php
# Update database settings
echo "Updating database settings..."
sudo sed -i "s/database_name_here/wordpress/g" /var/www/wordpress/wp-config.php
sudo sed -i "s/username_here/wordpressuser/g" /var/www/wordpress/wp-config.php
sudo sed -i "s/password_here/wordpress_pass/g" /var/www/wordpress/wp-config.php
sudo sed -i "s/localhost/127.0.0.1/g" /var/www/wordpress/wp-config.php
sudo sed -i "s/utf8mb4/utf8/g" /var/www/wordpress/wp-config.php
sudo sed -i "/define('DB_COLLATE', '');/a define('FS_METHOD', 'direct');" /var/www/wordpress/wp-config.php
echo "Database settings have been updated!"
echo "WordPress has been successfully installed and configured!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment