Skip to content

Instantly share code, notes, and snippets.

@panchicore
Last active September 27, 2021 03:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panchicore/9117211 to your computer and use it in GitHub Desktop.
Save panchicore/9117211 to your computer and use it in GitHub Desktop.
Use NGINX as webserver to deploy apps with python
# install NGINX requirements
sudo apt-get install libpcre3-dev build-essential libssl-dev
# install NGINX
cd
mkdir src
cd src
wget http://nginx.org/download/nginx-1.4.5.tar.gz
tar xvf nginx-1.4.5.tar.gz
cd nginx-1.4.5
# (want HTTPS?) SEE ADITIONAL NGINX MODULES: http://wiki.nginx.org/Modules
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
make
sudo make install
# init script
sudo vim /etc/init.d/nginx
# paste https://gist.githubusercontent.com/panchicore/1098336/raw/c0f6272cb58336c8f019e19c99a164635fcce826/5.+etc.init.d.nginx
sudo chmod +x /etc/init.d/nginx
# start it
sudo /etc/init.d/nginx start
# powerful restart nginx
# add to the bash profile
alias nginxrestart='sudo kill `cat /var/run/nginx.pid`; sudo /etc/init.d/nginx start'
# add to start up
sudo /usr/sbin/update-rc.d -f nginx defaults
# organize our webserver tree to deploy multiple apps
sudo vim /usr/local/nginx/conf/nginx.conf
# paste master nginx conf
# config python
sudo apt-get install python-setuptools python-dev
sudo easy_install pip
pip install virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.sh
# make your structure to deploy multiple apps
# create or clone the project
# test the project without nginx proxing
# paste the nginx app conf in your conf app folder
# make symlink in site-enabled
# restart the nginx and now test in domain.com
# supervisord must start on reboot
sudo su
crontab -e
#and then paste:
@reboot unlink /tmp/supervisor.sock; /usr/local/bin/supervisord -c /etc/supervisord.conf
user www-data;
worker_processes 1;
error_log /home/ubuntu/server/logs/nginx-error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
access_log /home/ubuntu/server/logs/nginx-access.log;
default_type application/octet-stream;
keepalive_timeout 10;
tcp_nodelay on;
client_max_body_size 20m;
sendfile on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Directories
client_body_temp_path /tmp/client_body/ 2 2;
fastcgi_temp_path /tmp/fastcgi/;
proxy_temp_path /tmp/proxy/;
uwsgi_temp_path /tmp/uwsgi/;
include /etc/nginx/conf.d/*.conf;
include /home/ubuntu/server/sites-enabled/*;
}
upstream app_server_perro.com {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name perro.com;
charset utf-8;
access_log /home/ubuntu/www/perro.com/logs/nginx-access.log;
error_log /home/ubuntu/www/perro.com/logs/nginx-error.log;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# Redirect www subdomain
if ($host = 'www.perro.com' ) {
rewrite ^/(.*)$ http://perro.com/$1 permanent;
}
# Django admin media.
# location /media/ {
# alias /home/admin/.virtualenvs/perro.com/lib/python2.7/site-packages/django/contrib/admin/media/;
# }
# Site media
#location /static/ {
# alias /home/admin/www/perro.com/path-to-generated-statics/;
#}
# Finally, send all non-media requests to the Django server.
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_perro.com;
break;
}
}
}
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/home/ubuntu/server/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[program:perro-gunicorn]
command=/home/admin/.virtualenvs/perro.com/bin/python /home/admin/www/perro.com/perro/manage.py run_gunicorn --settings=<settings-file> --workers=4 --max-requests=50 --bind=127.0.0.1:8000
directory=<path-to-django-project>
user=ubuntu
autostart=true
autorestart=true
stdout_logfile=/home/admin/www/perro.com/logs/supervisord.log
redirect_stderr=true
environment=HOME=/home/ubuntu,USER=ubuntu
# IF WANT TO USE UWSGI: pip install uwsgi
[program:perro.com-uwsgi]
command=/home/ubuntu/.virtualenvs/perro.com/bin/uwsgi
-m
--http 0.0.0.0:8000
--processes 4
--wsgi-file /home/ubuntu/www/perro.com/path/to/wsgi.py
--pythonpath /home/ubuntu/www/perro.com
--pidfile /home/ubuntu/uwsgi.pid
--home /home/ubuntu/.virtualenvs/perro.com
--master
directory=/home/ubuntu/www/perro.com/perro
user=admin
autostart=true
autorestart=true
stdout_logfile=/home/ubuntu/www/perro.com/logs/uwsgi.log
redirect_stderr=true
environment=HOME=/home/ubuntu,USER=ubuntu
stopsignal=INT
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8
dpkg-reconfigure locales
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment