Created
May 5, 2018 01:31
-
-
Save nandomoreirame/5d481f671f1ad1b1d8af3322150dab31 to your computer and use it in GitHub Desktop.
Setup nginx server sell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Nginx - new server block | |
| # Functions | |
| ok() { echo -e "\033[1;32m$1 ✔ \033[0m"; } | |
| die() { echo -e "\033[1;31m$1 ✖ \033[0m"; exit 1; } | |
| # Variables | |
| USER=$1 | |
| USER_PASS="ym5W57vMTvQ6j4qHDsGxHJLf3vkyqZ" | |
| USER_PATH="/home/$USER" | |
| NGINX_REQUEST_URI='$request_uri' | |
| PHP_FPM_POOL_PATH='/etc/php/7.2/fpm/pool.d' | |
| NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available' | |
| NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled' | |
| WEB_USER='www-data' | |
| NGINX_SCHEME='$scheme' | |
| # Sanity check | |
| [ $(id -g) != "0" ] && die "Script must be run as root." | |
| [ $# != "1" ] && die "Usage: $(basename $0) domain.com" | |
| # create new user | |
| # adduser --system --group $USER | |
| # useradd -p `mkpasswd "$USER_PASS"` -d /home/"$USER" -m -g users -s /bin/bash "$USER" | |
| PASS=$(perl -e 'print crypt($ARGV[0], "password")' $USER_PASS) | |
| useradd -m -p $PASS $USER | |
| # Check if user already exists. | |
| grep -q "$USER" /etc/passwd | |
| if [ $? != "0" ] | |
| then | |
| die "User $USER was not created correctly. Please try again." | |
| fi | |
| # create folders /home/USER/logs and /home/USER/www | |
| mkdir -p $USER_PATH/{www,logs} | |
| # Create php-fpm pool.d/DOMAIN.conf file | |
| cat > $PHP_FPM_POOL_PATH/$USER.conf <<EOF | |
| [$USER] | |
| user = $USER | |
| group = $USER | |
| listen = /var/run/php/php7.2-fpm.sock | |
| listen.owner = www-data | |
| listen.group = www-data | |
| listen.mode = 0660 | |
| php_admin_value[disable_functions] = exec,passthru,shell_exec,system | |
| php_admin_flag[allow_url_fopen] = off | |
| pm = dynamic | |
| pm.max_children = 5 | |
| pm.start_servers = 2 | |
| pm.min_spare_servers = 1 | |
| pm.max_spare_servers = 3 | |
| chdir = / | |
| EOF | |
| # Create nginx config file | |
| cat > $NGINX_AVAILABLE_VHOSTS/$USER <<EOF | |
| # www to non-www | |
| server { | |
| # If user goes to www direc them to non www | |
| server_name *.$USER; | |
| return 301 $NGINX_SCHEME://$USER$NGINX_REQUEST_URI; | |
| } | |
| server { | |
| listen 80; | |
| listen [::]:80; | |
| access_log $USER_PATH/logs/access.log; | |
| error_log $USER_PATH/logs/error.log; | |
| root $USER_PATH/www; | |
| index index.php index.html index.htm; | |
| server_name $USER; | |
| location / { | |
| try_files $uri $uri/ /index.php?q=$uri&$args; | |
| } | |
| error_page 404 /404.html; | |
| error_page 500 502 503 504 /50x.html; | |
| location = /50x.html { | |
| root /usr/share/nginx/html; | |
| } | |
| location ~ \.php$ { | |
| include snippets/fastcgi-php.conf; | |
| fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; | |
| } | |
| location ~ /\.ht { | |
| deny all; | |
| } | |
| location /.git { | |
| deny all; | |
| } | |
| } | |
| EOF | |
| # Creating index.php file | |
| cat > $USER_PATH/www/index.php <<EOF | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>Welcome to $USER</title> | |
| </head> | |
| <body> | |
| <header> | |
| <h1>Welcome to $USER</h1> | |
| </header> | |
| <footer> | |
| <p>© $(date +%Y)</p> | |
| </footer> | |
| </body> | |
| </html> | |
| EOF | |
| # Changing permissions | |
| chown -R $USER:$WEB_USER $USER_PATH/www | |
| # Enable site by creating symbolic link | |
| ln -s $NGINX_AVAILABLE_VHOSTS/$USER $NGINX_ENABLED_VHOSTS/$USER | |
| # Restart | |
| # echo "Do you wish to restart nginx?" | |
| # select yn in "Yes" "No"; do | |
| # case $yn in | |
| # Yes ) /etc/init.d/nginx restart ; break;; | |
| # No ) exit;; | |
| # esac | |
| # done | |
| ok "Site Created for $USER" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment