Centos7+virtualenv+supervisor+python3+uwsgi+django+nginx environment deployment

Keywords: Python supervisor Django vim

Viralenv and supervisor are installed in centos 2.7.5, but my project uses Python 3.6, so I need to compile Python 3 myself.
If you use virtualenv as Python 3, make sure that uwsgi also uses Python 3! Otherwise, there will be an ImportError: no module named site error, which is caused by the difference between the home env environment in your uwsgi configuration and the python version that uwsgi runs!

centos7 of the system I use

# Uninstall uwsgi of Python 2.75 in CentOS 7
pip uninstall uwsgi
# The next step is to move Python 2's uwsgi directly to any directory because sometimes the pip uninstall is not clean
mv /usr/bin/uwsgi ~

My project catalogue is as follows

.
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── __pycache__
│   ├── templatetags
│   ├── tests.py
│   └── views.py
├── django_blog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── README.md
├── requirements.txt
├── static
│   ├── css
│   ├── js
│   └── lib
├── templates
│   ├── 404.html
│   ├── about.html
│   ├── archives.html
│   ├── base.html
│   ├── index.html
│   ├── nav.html
│   ├── post.html
│   └── tag.html
├── uwsgi.ini
└── uwsgi.sock

1. Install uwsgi and configure uwsgi.ini file

# This step is in the global environment of the system (3.6)
pip3 install uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
# Create your uwsgi.ini file under project
vim uwsgi.ini
[uwsgi]
uid = root
gid = root
chmod-socket = 777
basedir=/usr/local
# Project directory
chdir=%(basedir)/django_blog
# application of the specified project
module=django_blog.wsgi:application
# virtualenv path 
home=/usr/local/env_django
# Enabling the main process
master=true
# Process quantity 
processes=2
# Specify the file path for sock
socket=/usr/local/django_blog/uwsgi.sock
# Clean up the environment and quit
vacuum=true
# Log files (you don't need to open them if you use supervisor management)
#daemonize=/var/log/uwsgi.log
# Start uwsgi
uwsgi -ini uwsgi.ini
# Check to see if the startup was successful
ps -ef | grep uwsgi

2. Using supervisor to manage uwsgi

#Redirect configuration files to directories
echo_supervisord_conf > /etc/supervisord.conf 
chmod +x /etc/supervisord.conf

Modify the final import file of the configuration file slightly

mkdir /etc/supervisor
vim /etc/supervisord.conf

Modify the last two lines of supervisord.conf to modify the import configuration file

[include]
files = /etc/supervisor/*.conf

Start supervisor

supervisord -c /etc/supervisord.conf

Adding configuration files

vim /etc/supervisor/django.conf
[program:django]
directory  = /usr/local/django_blog ;Command Execution Directory
command = /usr/bin/uwsgi --ini /usr/local/django_blog/uwsgi.ini
autostart = true   ;Default follows supervisord Autostart, defaulttrue
startsecs = 5      ; start-up 5 No abnormal exit seconds later, as if it had started normally
autorestart = true ;Automatic restart after program abnormal exit
startretries = 3   ; The number of auto-retries for startup failures, defaulted to be __________ 3
redirect_stderr = true  ;Redirection error
stdout_logfile = /var/log/uwsgi.log ;Here the log needs to be created manually or it cannot be started

Enter supervisorctl overload configuration

reload

3. Configure the permissions of nginxuwsgi.sock, and note that after closing debug**, django does not parse static files, which need to be parsed by nginx.
Note here, and remember to use Python management. py collectstatic or django admin's static file will be 404**

        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass unix:///usr/local/django_blog/uwsgi.sock;
        }

        location /static {
            expires 15d;
            autoindex on;
            alias /usr/local/django_blog/static/;
        }
systemctl start nginx

Posted by William on Wed, 06 Feb 2019 03:36:16 -0800