|
| 1 | +#!/bin/bash |
| 2 | +################################################# |
| 3 | +# # |
| 4 | +# A shell script to install Nginx and Flask # |
| 5 | +# # |
| 6 | +################################################# |
| 7 | + |
| 8 | +# check if the current user is root |
| 9 | +if [[ $(/usr/bin/id -u) != "0" ]]; then |
| 10 | + echo -e "This looks like a 'non-root' user.\nPlease switch to 'root' and run the script again." |
| 11 | + exit |
| 12 | +fi |
| 13 | + |
| 14 | +domain=1ドル |
| 15 | +username=$(echo $domain | cut -d. -f1) |
| 16 | +ipaddr=$(hostname -I) |
| 17 | + |
| 18 | +check_domain() { |
| 19 | + if [[ $domain == '' ]]; then |
| 20 | + echo -e "Usage: ./install_nginx domain-name.tld" |
| 21 | + exit |
| 22 | + elif [[ $(grep -o "\." <<< "$domain" | wc -l) > 1 || $(grep -o "\." <<< "$domain" | wc -l) < 1 ]]; then |
| 23 | + echo -e "Invalid domain! Usage: ./install_nginx main-domain-name.tld" |
| 24 | + exit |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +create_user() { |
| 29 | + useradd $username |
| 30 | + chmod go+x /home/$username |
| 31 | + |
| 32 | + mkdir /home/$username/logs |
| 33 | + mkdir /home/$username/public_html |
| 34 | + |
| 35 | + chcon -Rvt httpd_log_t /home/$username/logs/ |
| 36 | + chcon -Rvt httpd_sys_content_t /home/$username/public_html/ |
| 37 | + |
| 38 | + echo -e "<html>\n<head>\n\t<title>NGINX - TEST</title>\n</head>\n<body>\n\t<h3>THIS IS A TEST<h3>\n\t<h4>Index file loaded from /home/$username/public_html/<h4>\n</body>\n</html>" >> /home/$username/public_html/index.html |
| 39 | + |
| 40 | + chown -Rv $username:$username /home/$username |
| 41 | + usermod -aG wheel $username |
| 42 | +} |
| 43 | + |
| 44 | +create_vhost() { |
| 45 | + cp -fv /etc/nginx/nginx.conf "/etc/nginx/nginx.conf_bak-$(date +"%m-%d-%y")" |
| 46 | + |
| 47 | + cat > /etc/nginx/sites-available/$username.com.conf <<nginx |
| 48 | +server { |
| 49 | + listen 80; |
| 50 | + |
| 51 | + server_name $domain www.$domain; |
| 52 | + access_log /home/$username/logs/access.log; |
| 53 | + error_log /home/$username/logs/error.log; |
| 54 | + |
| 55 | + location / { |
| 56 | + proxy_set_header Host \$http_host; |
| 57 | + proxy_set_header X-Real-IP \$remote_addr; |
| 58 | + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
| 59 | + proxy_set_header X-Forwarded-Proto \$scheme; |
| 60 | + proxy_pass http://unix:/home/$username/public_html/$username.sock; |
| 61 | + } |
| 62 | + |
| 63 | + error_page 500 502 503 504 /50x.html; |
| 64 | + location = /50x.html { |
| 65 | + root /usr/share/nginx/html; |
| 66 | + } |
| 67 | +} |
| 68 | +nginx |
| 69 | + |
| 70 | + ln -sv /etc/nginx/sites-available/$username.com.conf /etc/nginx/sites-enabled/$username.com.conf |
| 71 | + nginx -t |
| 72 | + systemctl restart nginx |
| 73 | +} |
| 74 | + |
| 75 | +install_flask_gunicorn() { |
| 76 | + |
| 77 | +su - $username <<'EOF' |
| 78 | + cd public_html |
| 79 | + python3 -m venv flask_demoenv |
| 80 | + source flask_demoenv/bin/activate |
| 81 | + git init |
| 82 | + git remote add origin https://github.com/rn4ir/flask-gunicorn-demo.git |
| 83 | + git pull origin master |
| 84 | + /usr/bin/yes | pip install -r requirements.txt |
| 85 | + deactivate |
| 86 | + logout |
| 87 | +EOF |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +configure_gunicorn(){ |
| 92 | + |
| 93 | + cat > /etc/systemd/system/$username.service <<service |
| 94 | +[Unit] |
| 95 | +Description=Gunicorn instance to serve $username |
| 96 | +After=network.target |
| 97 | + |
| 98 | +[Service] |
| 99 | +User=$username |
| 100 | +Group=nginx |
| 101 | +WorkingDirectory=/home/$username/public_html |
| 102 | +Environment="PATH=/home/$username/public_html/flask_demoenv/bin" |
| 103 | +ExecStart=/home/$username/public_html/flask_demoenv/bin/gunicorn --workers 3 --bind unix:$username.sock -m 007 run |
| 104 | + |
| 105 | +[Install] |
| 106 | +WantedBy=multi-user.target |
| 107 | +service |
| 108 | + |
| 109 | + systemctl enable $username |
| 110 | + systemctl start $username |
| 111 | + systemctl status $username |
| 112 | +} |
| 113 | + |
| 114 | +post_install(){ |
| 115 | + |
| 116 | + usermod -a -G $username nginx |
| 117 | + nginx -t |
| 118 | + systemctl restart nginx |
| 119 | + setenforce 0 |
| 120 | +} |
| 121 | + |
| 122 | +check_domain |
| 123 | +create_user |
| 124 | +create_vhost |
| 125 | + |
| 126 | +install_flask_gunicorn |
| 127 | +configure_gunicorn |
| 128 | +post_install |
| 129 | + |
| 130 | +echo -e "\n\nInstallation complete.\nDomain:$domain\nUsername:$username\nHome Directory:/home/$username\n\nAdd the following to your local 'hosts' file:\n$ipaddr\t$domain www.$domain\n\nExiting..." |
0 commit comments