Skip to content

Instantly share code, notes, and snippets.

@cgtarmenta
Last active October 30, 2023 17:47
Show Gist options
  • Save cgtarmenta/28b3fa0546cf00eafbb41156c1982da3 to your computer and use it in GitHub Desktop.
Save cgtarmenta/28b3fa0546cf00eafbb41156c1982da3 to your computer and use it in GitHub Desktop.
Ubuntu 20.04 WP boilerplate
#!/bin/bash
# Usage:
# sudo ./script_name.sh [--domain=example.com] [--db_name=wordpress] [--db_user=wordpressuser] [--db_pass=password] [--ftp_user=ftpuser] [--ftp_pass=generated_password]
# All parameters are optional and will default to the values specified in the script.
# Default values
DOMAIN="example.com"
DB_NAME="wordpress"
DB_USER="wordpressuser"
DB_PASS="password"
FTP_USER="ftpuser"
FTP_PASS=$(openssl rand -base64 12)
WP_HOME="http://$DOMAIN"
# Override variables from script parameters
while [ $# -gt 0 ]; do
case "$1" in
--domain=*)
DOMAIN="${1#*=}"
WP_HOME="http://${1#*=}"
;;
--db_name=*)
DB_NAME="${1#*=}"
;;
--db_user=*)
DB_USER="${1#*=}"
;;
--db_pass=*)
DB_PASS="${1#*=}"
;;
--ftp_user=*)
FTP_USER="${1#*=}"
;;
--ftp_pass=*)
FTP_PASS="${1#*=}"
;;
*)
echo "Invalid argument: $1"
exit 1
esac
shift
done
echo "Starting installation..."
# Update system packages
echo "Updating system packages..."
apt update && apt upgrade -y
# Install Nginx, MariaDB, PHP, and other required packages
echo "Installing required packages..."
apt install -y nginx mariadb-server php7.4-fpm php7.4-mysql php7.4-xml php7.4-gd php7.4-opcache php7.4-mbstring php7.4-curl php7.4-cli php7.4-zip php7.4-intl unzip curl git vsftpd
# Start and enable Nginx and MariaDB
echo "Starting and enabling Nginx and MariaDB..."
systemctl start nginx
systemctl enable nginx
systemctl start mariadb
systemctl enable mariadb
# Secure MariaDB installation
echo "Securing MariaDB installation..."
mysql_secure_installation
# Create a new MariaDB database and user for WordPress
echo "Creating MariaDB database and user for WordPress..."
mysql -u root -p -e "CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
mysql -u root -p -e "GRANT ALL ON $DB_NAME.* TO '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';"
mysql -u root -p -e "FLUSH PRIVILEGES;"
# Download and configure WordPress
echo "Downloading and configuring WordPress..."
cd /var/www/
git clone https://github.com/WordPress/WordPress.git
cd WordPress
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cp wp-config-sample.php wp-config.php
sed -i "s/database_name_here/$DB_NAME/" wp-config.php
sed -i "s/username_here/$DB_USER/" wp-config.php
sed -i "s/password_here/$DB_PASS/" wp-config.php
echo "define( 'WP_HOME', '$WP_HOME' );" >> wp-config.php
echo "define( 'WP_SITEURL', WP_HOME );" >> wp-config.php
echo "define( 'WP_CONTENT_DIR', '/var/www/WordPress/wp-content' );" >> wp-config.php
chown -R www-data:www-data /var/www/WordPress
chmod -R 755 /var/www/WordPress
# Configure Nginx for WordPress
echo "Configuring Nginx for WordPress..."
cat > /etc/nginx/sites-available/$DOMAIN <<EOL
server {
listen 80;
server_name $DOMAIN www.$DOMAIN;
root /var/www/WordPress;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOL
ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
nginx -t && systemctl restart nginx
# Set up FTP server
echo "Setting up FTP server..."
echo -e "$FTP_PASS\n$FTP_PASS" | adduser --home /var/www/WordPress --shell /usr/sbin/nologin --ingroup www-data $FTP_USER
echo "$FTP_USER" > /etc/vsftpd.userlist
chown -R $FTP_USER:www-data /var/www/WordPress
find /var/www/WordPress -type d -exec chmod 775 {} \;
find /var/www/WordPress -type f -exec chmod 664 {} \;
sed -i 's/#write_enable=YES/write_enable=YES/' /etc/vsftpd.conf
sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' /etc/vsftpd.conf
echo "userlist_enable=YES" >> /etc/vsftpd.conf
echo "userlist_file=/etc/vsftpd.userlist" >> /etc/vsftpd.conf
echo "userlist_deny=NO" >> /etc/vsftpd.conf
systemctl restart vsftpd
echo "Installation and configuration complete!"
echo "FTP user: $FTP_USER"
echo "FTP password: $FTP_PASS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment