Skip to content

Instantly share code, notes, and snippets.

@bennof
Last active August 30, 2022 01:03
Show Gist options
  • Save bennof/45faf398cd3bbfbae2e1a0b0ae23c96c to your computer and use it in GitHub Desktop.
Save bennof/45faf398cd3bbfbae2e1a0b0ae23c96c to your computer and use it in GitHub Desktop.
Install WordPress, Nginx, MariaDB on Ubuntu or Debian
#!/bin/sh
# Install Wordpress, Nginx and MariaDB on Debian/Ubuntu
# (c) 2020 Benjamin 'Benno' Falkner
set -e # halt on error
cat <<EOF
Install WordPress with Nginx and MariaDB
(c) 2020 Benjamin 'Benno' Falkner (MIT-License)
EOF
## Settings
# all settings are in this section
URL=wp.host.local # change !!!!
### DB (MariaDB)
DB=mariadb # do not change
DB_NAME=wp # DB Name
DB_USER=wp_user # DB User
DB_PASSWD=$(date +%s | sha256sum | base64 | head -c 32 ; echo) # DB Password - change this!!!!
### MOODLE
WP_TGZ=http://wordpress.org/latest.tar.gz
WP_DIR=/var/www/wordpress
WP_USER=www-data # must exist and be member of www-data
## Run - Do not change below this line
help(){
cat <<EOF
usage: InstallWordPressNginxMariaDBonUbuntu.sh <args>
-u= --url=[url] set url for server (default: $URL)
-v --verbose verbose mode
other switches are not listed
EOF
}
# Read arguments
for i in "$@"; do
case $i in
-u=*|--url=*) URL="${i#*=}"; shift;;
--db-name=*) DB_NAME="${i#*=}"; shift;;
--db-user=*) DB_USER="${i#*=}"; shift;;
--db-password=*) DB_PASSWD="${i#*=}"; shift;;
--wp-source=*) WP_TGZ="${i#*=}"; shift;;
--wp-directory=*) WP_DIR="${i#*=}"; shift;;
--wp-user=*) WP_USER="${i#*=}"; shift;;
-v|--verbose) set -x; shift;;
-h|--help) help(); shift;;
*) ;;# unknown option;;
esac
done
### Update
sudo apt-get update
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
sudo systemctl restart php-fpm
### Install Nginx
sudo apt-get install nginx
sudo sh -c "cat > /etc/nginx/sites-available/$URL" <<EOF
server{
listen 80;
listen [::]:80;
server_name $URL;
root $WP_DIR;
index index.php index.html index.htm;
error_log /var/log/nginx/${URL}_error.log;
access_log /var/log/nginx/${URL}_access.log;
client_max_body_size 100M;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files \$uri \$uri/ /index.php?$args =404;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
EOF
## Install and configure MariaDB
sudo apt-get install mariadb-server
#sudo mysql_secure_installation
cat << EOF | sudo mysql -u root -p
CREATE DATABASE IF NOT EXISTS $DB_NAME default character set utf8;
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWD';
GRANT ALL ON $DB_NAME.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
EOF
## Install WordPresss
wget -c $WP_TGZ
sudo tar -zxf $(basename $WP_TGZ) -C $(dirname $WP_DIR)/
rm -rf $(basename $WP_TGZ)
sudo cp $WP_DIR/wp-config-sample.php $WP_DIR/wp-config.php
sudo ed $WP_DIR/wp-config.php << EOF
/define( 'DB_NAME',
c
define( 'DB_NAME', '$DB' );
.
/define( 'DB_USER',
c
define( 'DB_USER', '$DB_USER' );
.
/define( 'DB_PASSWORD',
c
define( 'DB_PASSWORD', '$DB_PASSWD' );
.
/define( 'AUTH_KEY',
.,.+8d
i
$(curl https://api.wordpress.org/secret-key/1.1/salt/)
.
wq
EOF
sudo chown $WP_USER:www-data -R $WP_DIR
sudo chmod 775 -R $WP_DIR
## enable all
sudo ln -s /etc/nginx/sites-available/$URL /etc/nginx/sites-enabled/$URL
sudo nginx -t
sudo systemctl reload nginx
@bajpangosh
Copy link

please add cloudflare or let's encrypt ssl support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment