Created
February 25, 2016 12:10
-
-
Save YieldNull/34a1d56d8316d4c0e737 to your computer and use it in GitHub Desktop.
Build A Website With Python-Flask and Nginx
This file contains 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 | |
# Build A Website With Python-Flask and Nginx | |
# Doc: http://yieldnull.com/blog/0b2e0169df807bec78b3ad3eea87105a2e5299c6/ | |
# http://yieldnull.com/blog/cd7be487eb5148eb4aa905543c1fd1396b618a6b/ | |
# Author: YieldNull | |
# Updated On: 2016/02/25 | |
if [ ! $# = 3 ] ; then | |
echo "Args: project_name port domain_name" | |
exit | |
fi | |
project=$1 # project name | |
port=$2 # project port | |
domain=$3 # domain name | |
# program directory | |
directory="/srv/www/$1" | |
sudo mkdir -p $directory/app | |
sudo mkdir -p $directory/log | |
sudo mkdir -p $directory/files | |
# nginx | |
nginx_file="/etc/nginx/sites-available/$project" | |
nginx_conf="server { | |
listen 80; | |
root $directory/app; | |
access_log $directory/log/access_log; | |
error_log $directory/log/error_log; | |
server_name $3; | |
location ~ ^\/static\/.*$ { | |
root $directory/app; | |
} | |
location ~ ^\/files\/.*$ { | |
root $directory/files; | |
} | |
location / { | |
proxy_pass http://127.0.0.1:$port; | |
proxy_set_header X-Real-IP \$remote_addr; | |
proxy_set_header Host \$host; | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
} | |
}" | |
echo "$nginx_conf" | sudo tee $nginx_file | |
sudo chmod 644 $nginx_file | |
sudo ln -s $nginx_file /etc/nginx/sites-enabled/$project | |
# supervisor | |
supervisor_file="/etc/supervisor/conf.d/$project.conf" | |
supervisor_conf=" | |
[program:$project] | |
command = $directory/venv/bin/gunicorn --bind 127.0.0.1:$port --workers 1 --worker-class gevent app:app | |
directory = $directory/app | |
user = git | |
startsecs = 3 | |
redirect_stderr = true | |
stdout_logfile_maxbytes = 50MB | |
stdout_logfile_backups = 10 | |
stdout_logfile = $directory/log/supervisor.log" | |
echo "$supervisor_conf" | sudo tee $supervisor_file | |
# virtualenv | |
sudo virtualenv $directory/venv | |
sudo chmod -R 777 $directory/venv | |
source $directory/venv/bin/activate | |
pip install gunicorn gevent | |
deactivate | |
sudo chmod -R 755 $directory/venv | |
sudo chown -R www-data:www-data $directory | |
sudo chown -R git:git $directory/app | |
sudo chown -R git:git $directory/venv | |
# git | |
git_dir="/srv/git/$project.git" | |
hook_file="$git_dir/hooks/post-receive" | |
hook_shell="#!/bin/bash | |
read oldrev newrev refname | |
if [ "'"$refname"'" = "\"refs/heads/master\"" ] ; then | |
# if master branch is pushed | |
GIT_WORK_TREE=$directory/app git checkout -f | |
source $directory/venv/bin/activate | |
pip install -r $directory/app/requirements.txt | |
deactivate | |
supervisorctl reload $project | |
fi | |
" | |
sudo mkdir -p /srv/git | |
sudo git init --bare $git_dir | |
echo "$hook_shell" | sudo tee $hook_file | |
sudo chmod 755 $hook_file | |
sudo chown -R git:git $git_dir | |
sudo chown -R $USER:$USER $directory/app | |
git clone git@127.0.0.1:/srv/git/$project.git $directory/app | |
sudo chown -R git:git $directory/app | |
sudo service nginx restart | |
sudo supervisorctl reload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment