Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active December 10, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/8f19a14c852651482b975635b52728f5 to your computer and use it in GitHub Desktop.
Save wpsmith/8f19a14c852651482b975635b52728f5 to your computer and use it in GitHub Desktop.
Bash Shell: Sets up an AWS Linux 2 Server for WordPress using NGINX.
#!/bin/bash
##############################################################
# Set Your System and Wordpress Config Preferences
##############################################################
export SYSTEM_USER=nginx # User PHP-FPM runs under
export SYSTEM_GROUP=www # User PHP-FPM runs under
##########################
# Start the setup and install
##########################
# Run system updates
yum update -y
# Install NGINX 1.12
amazon-linux-extras install nginx1.12
# Install PHP 7.2
amazon-linux-extras install php7.2
# Install MariaDB (MySQL replacement)
yum install -y mariadb-server mariadb
# Configure PHP-FPM instance to run as the user created (replace <USERNAME> with the user you used in the last step)
cat << EOF > /etc/php-fpm.d/www.conf
[www]
user = $SYSTEM_USER
group = $SYSTEM_GROUP
listen = /run/php-fpm/www.sock
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
EOF
# Configure NGINX
cat << EOF > /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
EOF
# remove un-used files
rm -f /etc/nginx/conf.d/php-fpm.conf
rm -f /etc/nginx/default.d/php.conf
# Start services and set to start on boot
systemctl start mariadb
systemctl enable mariadb
systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm
sleep 15
# Secure MariaDB with a Random Password and save it in /root/.my.cnf
# Also setup Wordpress DB
SQLROOTPASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;`
mysql -u root <<-EOF
UPDATE mysql.user SET Password=PASSWORD('`echo $SQLROOTPASS`') WHERE User='root';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.db WHERE Db='test' OR Db='test_%';
DROP DATABASE test;
FLUSH PRIVILEGES;
EOF
cat << EOF > ~/.my.cnf
[client]
password=`echo $SQLROOTPASS`
EOF
sleep 5
# Install WP-CLI (wp-cli.org)
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
#!/bin/bash
##############################################################
# Set Your System and Wordpress Config Preferences
##############################################################
export SYSTEM_USER=nginx # User PHP-FPM runs under
export SYSTEM_GROUP=www # User PHP-FPM runs under
# Database
export WP_DB_NAME=fifabot_prod #replace
export WP_DB_USER=fifabot_prod #replace
WP_DB_PASS=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;`
export WP_DB_PASS=$WP_DB_PASS
export WP_DB_PREFIX=wp_ # Only numbers, letters, and underscores please!
# Site info
export SITE_URL=fifabot.app #replace
export SITE_TITLE="My Great Wordpress Site"
# Wordpress Login Info
export ADMIN_USERNAME=wpsmith #replace
export ADMIN_PASSWORD=password #replace
export ADMIN_EMAIL=t@wpsmith.net #replace
# I like to setup my websites under /var/www/vhosts/domain.com/html It's how I roll
mkdir -p /var/www/vhosts/$SITE_URL/html
# Secure MariaDB with a Random Password and save it in /root/.my.cnf
# Also setup Wordpress DB
mysql -u root <<-EOF
CREATE DATABASE $WP_DB_NAME;
grant all on $WP_DB_NAME.* to $WP_DB_USER@'localhost' identified by '$WP_DB_PASS';
FLUSH PRIVILEGES;
EOF
export PATH=$PATH:/usr/local/bin/
# Install Wordpress as the SYSTEM_USER
su $SYSTEM_USER
cd /var/www/vhosts/$SITE_URL/html
wp core download
wp config create --dbname=$WP_DB_NAME --dbuser=$WP_DB_USER --dbpass=$WP_DB_PASS --dbprefix=$WP_DB_PREFIX
wp core install --url=$SITE_URL --title="$SITE_TITLE" --admin_user=$ADMIN_USERNAME --admin_password=$ADMIN_PASSWORD --admin_email=$ADMIN_EMAIL
# Fix permissions
find /var/www/vhosts/$SITE_URL -type d -exec chmod 755 {} \;
find /var/www/vhosts/$SITE_URL -type f -exec chmod 664 {} \;
chmod 600 /var/www/vhosts/$SITE_URL/html/wp-config.php
chmod 600 /var/www/vhosts/$SITE_URL/html/.htaccess
#!/bin/bash
export SYSTEM_USER=nginx # User PHP-FPM runs under
export SYSTEM_GROUP=www # User PHP-FPM runs under
# Site info
export SITE_URL=example.com
export PATH=$PATH
# Fix permissions
find /var/www/vhosts/ -type d -exec chmod 755 {} \;
find /var/www/vhosts/ -type f -exec chmod 664 {} \;
find -name wp-config.php -exec chmod 600 {} \;
find -name .htaccess -exec chmod 600 {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment