Created
November 23, 2015 15:13
-
-
Save Beagon/15e27040a7a29b88b751 to your computer and use it in GitHub Desktop.
[BASH][NGINX][V1.0] Create Project
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
| #!/bin/bash | |
| WEBSITE_DIRECTORY="/var/www/" | |
| NGINX_DIRECTORY="/etc/nginx/" | |
| MYSQL_USER="root" | |
| MYSQL_PASSWORD="" | |
| #DON'T EDIT ANYTHING UNDERNEATH HERE UNLESS YOU KNOW WHAT YOU ARE DOING! | |
| checkWebsiteDirectoryConfig() | |
| { | |
| if [ -z "${WEBSITE_DIRECTORY}" ] | |
| then | |
| echo "[To disable this message please fill in WEBSITE_DIRECTORY in the script]" | |
| read -p "WEBSITE_DIRECTORY is empty! Please fill in your website directory e.g. /var/www/: " WEBSITE_DIRECTORY | |
| checkWebsiteDirectoryConfig | |
| else | |
| checkNginxDirectoryConfig | |
| fi | |
| } | |
| checkNginxDirectoryConfig() | |
| { | |
| if [ -z "${NGINX_DIRECTORY}" ] | |
| then | |
| echo "[To disable this message please fill in NGINX_DIRECTORY in the script]" | |
| read -p "NGINX_DIRECTORY is empty! Please fill in your nginx directory e.g. /etc/nginx/: " NGINX_DIRECTORY | |
| checkNginxDirectoryConfig | |
| else | |
| checkMysqlUserConfig | |
| fi | |
| } | |
| checkMysqlUserConfig() | |
| { | |
| if [ -z "${MYSQL_USER}" ] | |
| then | |
| echo "[To disable this message please fill in MYSQL_USER in the script]" | |
| read -p "MYSQL_USER is empty! Please fill in your mysql username: " MYSQL_USER | |
| checkMysqlUserConfig | |
| else | |
| checkMysqlPasswordConfig | |
| fi | |
| } | |
| checkMysqlPasswordConfig() | |
| { | |
| if [ -z "${MYSQL_PASSWORD}" ] | |
| then | |
| echo "[To disable this message please fill in MYSQL_PASSWORD in the script]" | |
| read -p "MYSQL_PASSWORD is empty! Please fill in your mysql password: " MYSQL_PASSWORD | |
| checkMysqlUserConfig | |
| fi | |
| } | |
| getVhostName() | |
| { | |
| read -p "Please enter a domain name: " vhostName | |
| if [ -z "${vhostName}" ] | |
| then | |
| echo "Please fill in a domain name!" | |
| getVhostName | |
| else | |
| if [ -z "${USER_USERNAME}" ] | |
| then | |
| WEB_DIR=$WEBSITE_DIRECTORY/$vhostName | |
| else | |
| WEB_DIR=/home/$USER_USERNAME/$vhostName | |
| fi | |
| fi | |
| } | |
| getCmsSystem() | |
| { | |
| read -p "Please enter what kind of CMS you're setting up (none, drupal, modx, symfony2): " cmsSystem | |
| if [ -z "${cmsSystem,,}" ] | |
| then | |
| getCmsSystem | |
| else | |
| case $cmsSystem in | |
| drupal) | |
| buildDrupalVhost | |
| ;; | |
| modx) | |
| buildModxVhost | |
| ;; | |
| symfony2) | |
| buildSymfony2Vhost | |
| ;; | |
| none) | |
| buildNormalVhost | |
| ;; | |
| *) | |
| echo "This is not a valid CMS system!" | |
| getCmsSystem | |
| ;; | |
| esac | |
| fi | |
| } | |
| setUserDetails() | |
| { | |
| if [ -z "${USER_USERNAME}" ] | |
| then | |
| read -p "Username for the user: " USER_USERNAME | |
| fi | |
| USER_PASSWORD=$(date +%s|sha256sum|base64|head -c 16) | |
| } | |
| createUser() | |
| { | |
| read -p "Do you want to create an user for this domain? (Y)es/(N)o:" -r | |
| if [[ $REPLY =~ ^[Yy]$ ]] | |
| then | |
| setUserDetails | |
| useradd -g users -d /home/$USER_USERNAME -s /bin/bash -p $(echo "${USER_PASSWORD}" | openssl passwd -1 -stdin ) -m $USER_USERNAME | |
| elif [[ $REPLY =~ ^[Nn]$ ]] | |
| then | |
| return | |
| else | |
| echo "${REPLY} is not a valid option!" | |
| createUser | |
| fi | |
| } | |
| setMysqlDetails() | |
| { | |
| if [ -z "${NMYSQL_USERNAME}" ] | |
| then | |
| read -p "MySQL username for the user: " NMYSQL_USERNAME | |
| fi | |
| NMYSQL_PASSWORD=$(date +%s|sha256sum|base64|head -c 16) | |
| } | |
| createMysqlUser() | |
| { | |
| read -p "Do you want to create an mysql user for this domain? (Y)es/(N)o:" -r | |
| if [[ $REPLY =~ ^[Yy]$ ]] | |
| then | |
| setMysqlDetails | |
| mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE USER '${NMYSQL_USERNAME}'@'localhost' IDENTIFIED BY '${NMYSQL_PASSWORD}';" | |
| mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON \`${NMYSQL_USERNAME}\_%\` . * TO '${NMYSQL_USERNAME}'@'localhost';" | |
| mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH PRIVILEGES;" | |
| elif [[ $REPLY =~ ^[Nn]$ ]] | |
| then | |
| return | |
| else | |
| echo "${REPLY} is not a valid option!" | |
| createMysqlUser | |
| fi | |
| } | |
| createDirAndVhostFiles() | |
| { | |
| echo "Creating directory " | |
| mkdir -p "${WEB_DIR}/public_html" | |
| mkdir -p "${WEB_DIR}/logs" | |
| if [ -z "${USER_USERNAME}" ] | |
| then | |
| chown -R www-data:www-data "${WEB_DIR}" | |
| else | |
| chown -R $USER_USERNAME:www-data "${WEB_DIR}" | |
| fi | |
| chmod 755 "${WEB_DIR}/public_html" | |
| chmod 777 "${WEB_DIR}/logs" | |
| echo "Directories created." | |
| echo "===================" | |
| echo "Creating vHost file" | |
| touch "${NGINX_DIRECTORY}sites-available/${vhostName}" | |
| echo "vHost created" | |
| ln -s "${NGINX_DIRECTORY}sites-available/${vhostName}" "${NGINX_DIRECTORY}sites-enabled/${vhostName}" | |
| echo "Symlink created" | |
| echo "===================" | |
| } | |
| buildDrupalVhost() | |
| { | |
| echo "Drupal selected." | |
| echo "===================" | |
| createDirAndVhostFiles | |
| echo "Filling in vHost" | |
| echo " | |
| server { | |
| server_name ${vhostName} www.${vhostName}; | |
| root ${WEB_DIR}/public_html; | |
| gzip_static on; | |
| location = /favicon.ico { | |
| log_not_found off; | |
| access_log off; | |
| } | |
| location = /robots.txt { | |
| allow all; | |
| log_not_found off; | |
| access_log off; | |
| } | |
| location ~* \\.(txt|log)\$ { | |
| allow 192.168.0.0/16; | |
| deny all; | |
| } | |
| location ~ \\..*/.*\\.php\$ { | |
| return 403; | |
| } | |
| location ~ ^/sites/.*/private/ { | |
| return 403; | |
| } | |
| location ~ (^|/)\\. { | |
| return 403; | |
| } | |
| location / { | |
| try_files \$uri @rewrite; | |
| } | |
| location @rewrite { | |
| rewrite ^ /index.php; | |
| # For Drupal 6 and below: | |
| # Some modules enforce no slash (/) at the end of the URL | |
| # Else this rewrite block wouldn't be needed (GlobalRedirect) | |
| #rewrite ^/(.*)\$ /index.php?q=\$1; | |
| } | |
| location ~ \\.php\$ { | |
| include /etc/nginx/fastcgi_params; | |
| fastcgi_pass unix:/var/run/php5-fpm.sock; | |
| fastcgi_index index.php; | |
| fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
| } | |
| # This is for D6 | |
| #location ~ ^/sites/.*/files/imagecache/ { | |
| # This is for D7 and D8 | |
| location ~ ^/sites/.*/files/styles/ { | |
| try_files \$uri @rewrite; | |
| } | |
| location ~* \\.(js|css|png|jpg|jpeg|gif|ico)\$ { | |
| expires max; | |
| log_not_found off; | |
| } | |
| error_log ${WEB_DIR}/logs/error.log; | |
| access_log ${WEB_DIR}/logs/access.log; | |
| }" > "${NGINX_DIRECTORY}sites-available/${vhostName}" | |
| echo "vHost filled in." | |
| } | |
| buildModxVhost() | |
| { | |
| echo "ModX selected" | |
| echo "===================" | |
| createDirAndVhostFiles | |
| echo "Filling in vHost" | |
| echo "server { | |
| listen 80; | |
| server_name ${vhostName} www.${vhostName}; | |
| root ${WEB_DIR}/public_html; | |
| index index.php; | |
| client_max_body_size 30M; | |
| location / { | |
| root ${WEB_DIR}/public_html; | |
| if (!-e \$request_filename) { | |
| rewrite ^/(.*)\$ /index.php?q=\$1 last; | |
| } | |
| } | |
| location ~ \\.php\$ { | |
| try_files \$uri =404; | |
| fastcgi_split_path_info ^(.+\\.php)(.*)\$; | |
| fastcgi_pass unix:/var/run/php5-fpm.sock; | |
| fastcgi_index index.php; | |
| fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
| include fastcgi_params; | |
| fastcgi_ignore_client_abort on; | |
| fastcgi_param SERVER_NAME \$http_host; | |
| } | |
| location ~ /\\.ht { | |
| deny all; | |
| } | |
| error_log ${WEB_DIR}/logs/error.log; | |
| access_log ${WEB_DIR}/logs/access.log; | |
| }" > "${NGINX_DIRECTORY}sites-available/${vhostName}" | |
| echo "vHost filled in." | |
| } | |
| buildNormalVhost() | |
| { | |
| echo "None selected" | |
| echo "===================" | |
| createDirAndVhostFiles | |
| echo "Filling in vHost" | |
| echo " | |
| server { | |
| listen 80; | |
| root ${WEB_DIR}/public_html; | |
| index index.html index.htm; | |
| server_name ${vhostName} www.${vhostName}; | |
| location ~ \\.php\$ { | |
| include /etc/nginx/fastcgi_params; | |
| fastcgi_pass unix:/var/run/php5-fpm.sock; | |
| fastcgi_index index.php; | |
| fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
| } | |
| error_log ${WEB_DIR}/logs/error.log; | |
| access_log ${WEB_DIR}/logs/access.log; | |
| }" > "${NGINX_DIRECTORY}sites-available/${vhostName}" | |
| echo "vHost filled in." | |
| } | |
| buildSymfony2Vhost() | |
| { | |
| echo "Symfony2 selected" | |
| echo "===================" | |
| createDirAndVhostFiles | |
| echo "Filling in vHost" | |
| echo " | |
| server { | |
| server_name ${vhostName} www.${vhostName}; | |
| root ${WEB_DIR}/public_html/web; | |
| location / { | |
| # try to serve file directly, fallback to app.php | |
| try_files \$uri /app.php\$is_args\$args; | |
| } | |
| location ~ ^/(app|app_dev|config)\\.php(/|\$) { | |
| fastcgi_pass unix:/var/run/php5-fpm.sock; | |
| fastcgi_split_path_info ^(.+\\\\.php)(/.*)\$; | |
| include fastcgi_params; | |
| fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
| fastcgi_param HTTPS off; | |
| } | |
| error_log ${WEB_DIR}/logs/error.log; | |
| access_log ${WEB_DIR}/logs/access.log; | |
| }" > "${NGINX_DIRECTORY}sites-available/${vhostName}" | |
| echo "vHost filled in." | |
| } | |
| restartNGINX() | |
| { | |
| echo "===================" | |
| echo "Reloading nginx..." | |
| service nginx reload | |
| echo "Nginx reloaded!" | |
| echo "If you didn't encounter any errors the vhost has been setup!" | |
| echo "===================" | |
| } | |
| showInfo() | |
| { | |
| echo "=====================================================" | |
| echo "! PLEASE COPY THIS INFO! IT WILL NOT BE SHOWN AGAIN !" | |
| echo "=====================================================" | |
| echo "The server details:" | |
| echo "Domain name: ${vhostName}" | |
| echo "Website root: ${WEB_DIR}/public_html" | |
| echo "Website logs: ${WEB_DIR}/logs" | |
| if [ -z "${USER_USERNAME}" ] | |
| then | |
| return | |
| else | |
| echo "Username: ${USER_USERNAME}" | |
| echo "Password: ${USER_PASSWORD}" | |
| fi | |
| if [ -z "${NMYSQL_USERNAME}" ] | |
| then | |
| return | |
| else | |
| echo "The MySQL details:" | |
| echo "Username: ${NMYSQL_USERNAME}" | |
| echo "Password: ${NMYSQL_PASSWORD}" | |
| fi | |
| echo "=====================================================" | |
| echo "! PLEASE COPY THIS INFO! IT WILL NOT BE SHOWN AGAIN !" | |
| echo "=====================================================" | |
| read something | |
| clear | |
| } | |
| init() { | |
| typeset -l cmsSystem | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "This script must be run as root!" 1>&2 | |
| exit 1 | |
| fi | |
| echo "NGINX Add Vhosts - Copyright Robin Rijkeboer 2015" | |
| checkWebsiteDirectoryConfig | |
| createUser | |
| getVhostName | |
| createMysqlUser | |
| getCmsSystem | |
| restartNGINX | |
| showInfo | |
| } | |
| init |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment