Skip to content

Instantly share code, notes, and snippets.

@anhtranbk
Last active December 9, 2016 07:55
Show Gist options
  • Save anhtranbk/896b443c630bd60577e67f528ad35cb1 to your computer and use it in GitHub Desktop.
Save anhtranbk/896b443c630bd60577e67f528ad35cb1 to your computer and use it in GitHub Desktop.
Setup flask application with uwsgi and nginx, mysql as database, run on Centos 6.5
#!/bin/sh
# Open port iptables only for CentOS 6
iptables -F
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
service iptables save
#!/bin/bash
# install some tools
#
sudo yum update -y
sudo yum install epel-release -y
sudo yum install build-essential gcc gcc-c++ wget python-devel -y
# install and config mysql-server
service mysqld status > /dev/null
if [ $? -ne 0 ]; then
sudo rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
sudo yum install mysql-server mysql-devel mysql-lib -y
sudo chkconfig mysqld on
sudo service mysqld start
sudo mysql_secure_installation
fi
# install and config nginx
service nginx status > /dev/null
if [ $? -ne 0 ]; then
sudo echo '# Add repo to install latest nginx on CentOS
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/nginx.repo
sudo yum install nginx -y
sudo cp piti.conf /etc/nginx/conf.d/piti.conf
sudo chown piti:piti /etc/nginx/conf.d/piti.conf
sudo chmod 660 /etc/nginx/conf.d/piti.conf
sudo chkconfig nginx on
sudo service nginx start
fi
# install python-pip
wget https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py
rm -f ez_setup.py
rm -f setuptools*.zip
sudo yum install -y python-pip
sudo pip install --upgrade pip
# install virtualenv and python dependencies
sudo pip install virtualenv
virtualenv env
source env/bin/activate
pip install -r requirements.txt
# Nginx configuration file
server {
listen 80;
server_name api.pitienglish.com;
charset utf-8;
access_log /var/log/piti/api_access.log main;
error_log /var/log/piti/api_error.log;
location ~ \.(zip) {
root /var/www/html/pitienglish.com/piti/uploads;
}
location / {
try_files $uri @yourapplication;
client_max_body_size 32MB;
}
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
uwsgi_read_timeout 30s;
uwsgi_send_timeout 30s;
}
}
#!/bin/bash
# Get function from functions library
. /etc/init.d/functions
BASE=/your/web/application/directory
UWSGI=$BASE/env/bin
start() {
echo -n "Starting server ..."
touch $BASE/reload
$UWSGI/uwsgi --ini $BASE/uwsgi.ini &
success $"Started"
echo
}
reload() {
$UWSGI/uwsgi --reload /tmp/uwsgi-master.pid
success $ "Reloaded"
echo
}
stop() {
echo -n "Stopping server ..."
$UWSGI/uwsgi --stop /tmp/uwsgi-master.pid
success $ "Stopped"
echo
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[uwsgi]
base = your/web/application/directory
python-path = %(base)
module = wsgi
callable = app
pidfile = /tmp/uwsgi-master.pid
touch-reload = %(base)/reload
master = true
processes = 5
enable-threads = true
limit-as = 512
# use unix socket because it is more secure and faster than TCP socket
socket = /tmp/uwsgi.sock
chmod-socket = 660
uid = nginx
gid = nginx
vacuum = true
die-on-term = true
emperor = true
logto = /var/log/piti/uwsgi.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment