Skip to content

Instantly share code, notes, and snippets.

@esradev
Created December 16, 2024 19:41
Show Gist options
  • Save esradev/cf4896c8566c2bf57a160889cce726a3 to your computer and use it in GitHub Desktop.
Save esradev/cf4896c8566c2bf57a160889cce726a3 to your computer and use it in GitHub Desktop.
install-wordpress.sh
# Gist: install_wordpress.sh
## Description
This script automates the process of setting up a new WordPress site on a server. It performs the following tasks:
1. Checks if a site name is provided as an argument.
2. Creates a MySQL database and user for the new site.
3. Sets up the web root directory.
4. Downloads and installs WordPress.
5. Configures the `wp-config.php` file with database details and security salts.
6. Configures Apache to serve the new WordPress site.
## File: install_wordpress.sh
```bash
# filepath: /home/wpstorm/Scripts/install_wordpress.sh
# Script to add new site automatically:
# Check if a site name is provided
if [ -z "$1" ]; then
echo "Usage: $0 <site-name>"
exit 1
fi
SITE_NAME=$1
WEB_ROOT="/var/www/html/$SITE_NAME"
DIR_ROOT="/var/www/html"
# Create MySQL database and user
MYSQL_ROOT_PASSWORD="Wpstorm#0601"
MYSQL_USER="${SITE_NAME}-user"
MYSQL_PASSWORD="${SITE_NAME}-Wpstorm#0601"
MYSQL_DATABASE="${SITE_NAME}-db"
sudo mysql -u root -p"$MYSQL_ROOT_PASSWORD" <<MYSQL_SCRIPT
CREATE DATABASE \`$MYSQL_DATABASE\`;
CREATE USER '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';
GRANT ALL PRIVILEGES ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'localhost';
FLUSH PRIVILEGES;
EXIT;
MYSQL_SCRIPT
# Create web root directory if it doesn't exist
sudo mkdir -p $WEB_ROOT
sudo chown -R $USER:$USER $WEB_ROOT
# Download and install WordPress
if [ ! -f "$DIR_ROOT/latest.tar.gz" ]; then
wget https://wordpress.org/latest.tar.gz -P $DIR_ROOT
fi
sudo tar -zxvf $DIR_ROOT/latest.tar.gz -C $WEB_ROOT --strip-components=1
sudo chown -R www-data:www-data $WEB_ROOT
sudo chmod -R 755 $WEB_ROOT
# Create wp-config.php from wp-config-sample.php
cp $WEB_ROOT/wp-config-sample.php $WEB_ROOT/wp-config.php
# Set database details with perl find and replace
perl -pi -e "s/database_name_here/$MYSQL_DATABASE/g" $WEB_ROOT/wp-config.php
perl -pi -e "s/username_here/$MYSQL_USER/g" $WEB_ROOT/wp-config.php
perl -pi -e "s/password_here/$MYSQL_PASSWORD/g" $WEB_ROOT/wp-config.php
# Set WP salts
SALT=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
STRING='wpstorm'
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s $WEB_ROOT/wp-config.php
# Configure Apache for WordPress
echo "<VirtualHost *:80>
ServerAdmin webmaster@$SITE_NAME
DocumentRoot $WEB_ROOT
ServerName $SITE_NAME
ServerAlias www.$SITE_NAME
<Directory $WEB_ROOT>
AllowOverride All
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/$SITE_NAME.conf
sudo a2ensite $SITE_NAME.conf
sudo a2enmod rewrite
sudo systemctl reload apache2 # Reload Apache to apply the configuration
echo "WordPress installation for $SITE_NAME is complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment