Skip to content

Instantly share code, notes, and snippets.

@fahdi
Forked from romanitalian/wp_make_new_site.sh
Created October 9, 2021 14:31
Show Gist options
  • Save fahdi/59d9345a91aca3e0a508d4b9236ab460 to your computer and use it in GitHub Desktop.
Save fahdi/59d9345a91aca3e0a508d4b9236ab460 to your computer and use it in GitHub Desktop.
BASH script to make new WP site: download and WP, configure and restart NGINX; install/make wordpress site by Bash script; wordpress install; wordpress configuration; wordpress initialisation
#!/bin/bash
# -------------------------------------------------
# Make site directory
# Download WP and install WP to site directory
# Set WP configuration
# Configure NGINX for new domain-name
# -------------------------------------------------
# apt-get update
# apt-get install curl
# apt-get install wget
# apt-get install nginx
# apt-get install vim
#
#
# /etc/init.d/nginx start
#
# https://support.rackspace.com/how-to/installing-mysql-server-on-ubuntu/
# y | apt-get install mysql-server
# systemctl start mysql
# systemctl enable mysql
#
# Set "root" pass:
# /usr/bin/mysql -u root -p
# UPDATE mysql.user SET authentication_string = PASSWORD('roman_root_123') WHERE User = 'root';
# FLUSH PRIVILEGES;
# exit;
#
# Configure DB helper (set "root" pass and etc):
# mysql_secure_installation utility ---- do not use it. It can be cause error to add mysql user
#
# apt-get install mysql-client
# mysql --version
#
# apt-get install php7.2
# apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php
# service php7.2-fpm start
clear
if [[ `id -u` != 0 ]]; then
echo >&2 "Must be root to run script";
exit;
fi
echo "-----------------------------------------------------"
echo "STEP #0/7";
cat << EOF
Example:
1/6. DOMAIN_NAME: my-site.com
2/6. DB_USER_NAME: roman
3/6. DB_USER_PASS: 123
4/6. DB_NAME: db_my_site_com
5/6. DB_ROOT_USER_NAME: root
6/6. DB_ROOT_USER_PASS: root_password
EOF
read -p "1/6. DOMANIN_NAME: " DOMAIN_NAME;
read -p "2/6. DB_USER_NAME: " DB_USER_NAME
read -p "3/6. DB_USER_PASS: " DB_USER_PASS
read -p "4/6. DB_NAME (example: db_demo): " DB_NAME
read -p "5/6. DB_ROOT_USER_NAME: " DB_ROOT_USER_NAME
read -p "6/6. DB_ROOT_USER_PASS: " DB_ROOT_USER_PASS
if [[ $DOMAIN_NAME == "" ]]; then
echo >&2 "DOMAIN_NAME is empty";
exit;
fi
if [[ $DB_ROOT_USER_NAME == "" ]]; then
echo >&2 "DB_ROOT_USER_NAME is empty";
exit;
fi
if [[ $DB_ROOT_USER_PASS == "" ]]; then
echo >&2 "DB_ROOT_USER_PASS is empty";
exit;
fi
if [[ $DB_USER_NAME == "" ]]; then
echo >&2 "DB_USER_NAME is empty";
exit;
fi
if [[ $DB_USER_PASS == "" ]]; then
echo >&2 "DB_USER_PASS is empty";
exit;
fi
if [[ $DB_NAME == "" ]]; then
echo >&2 "DB_NAME is empty";
exit;
fi
echo "-----------------------------------------------------"
echo "STEP #1/7 (make directories)"
mkdir -p /var/www/$DOMAIN_NAME/public;
chown -R www-data:www-data /var/www/$DOMAIN_NAME/public;
chmod -R 755 /var/www;
echo "-----------------------------------------------------"
echo "STEP #2/7 (download WP)"
rm -rf /tmp/wordpress
cd /tmp
wget -c -q http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
echo "-----------------------------------------------------"
echo "STEP #3/7 (install WP)"
cp -a /tmp/wordpress/. /var/www/$DOMAIN_NAME/public/
chown www-data:www-data -R /var/www/$DOMAIN_NAME/public/*
mkdir /var/www/$DOMAIN_NAME/public/wp-content/uploads
chmod 775 /var/www/$DOMAIN_NAME/public/wp-content/uploads
echo "-----------------------------------------------------"
echo "STEP #4/7 (configure DB)"
CMD="CREATE DATABASE $DB_NAME;"
echo $CMD;
mysql -u $DB_ROOT_USER_NAME -p$DB_ROOT_USER_PASS -e CMD
CMD="UPDATE mysql.user SET authentication_string = PASSWORD('$DB_USER_PASS') WHERE User = '$DB_USER_NAME';"
echo $CMD;
mysql -u $DB_ROOT_USER_NAME -p$DB_ROOT_USER_PASS -e CMD
CMD="GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER_NAME'@"localhost" IDENTIFIED BY '$DB_USER_PASS';"
echo $CMD;
mysql -u $DB_ROOT_USER_NAME -p$DB_ROOT_USER_PASS -e CMD
CMD="FLUSH PRIVILEGES;"
echo $CMD;
mysql -u $DB_ROOT_USER_NAME -p$DB_ROOT_USER_PASS -e CMD
echo "-----------------------------------------------------"
echo "STEP #5/7 (configure WP 'DB connection')"
cp /var/www/$DOMAIN_NAME/public/wp-config-sample.php /var/www/$DOMAIN_NAME/public/wp-config.php
# define( 'DB_NAME', 'database_name_here' );
# define( 'DB_USER', 'username_here');
# define( 'DB_PASSWORD', 'password_here' );
sed -i -e "s/database_name_here/$DB_NAME/g" /var/www/$DOMAIN_NAME/public/wp-config.php
sed -i -e "s/username_here/$DB_USER_NAME/g" /var/www/$DOMAIN_NAME/public/wp-config.php
sed -i -e "s/password_here/$DB_USER_PASS/g" /var/www/$DOMAIN_NAME/public/wp-config.php
echo "-----------------------------------------------------"
echo "STEP #6/7 (configure NGINX)"
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/$DOMAIN_NAME
cat << EOF > /etc/nginx/sites-available/$DOMAIN_NAME
server {
listen 80;
listen [::]:80;
root /var/www/$DOMAIN_NAME/public;
index index.php index.html index.htm;
server_name $DOMAIN_NAME www.$DOMAIN_NAME;
location / {
try_files $uri $uri/ /index.php$is_args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index ndex.php;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
EOF
ln -s /etc/nginx/sites-available/$DOMAIN_NAME /etc/nginx/sites-enabled/
echo "-----------------------------------------------------"
echo "STEP #7/7 (test NGINX configuration and restart NGINX)\n"
nginx -t
echo "RESTART NGINX"
systemctl restart nginx
# wp-admin/install.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment