Nginx+Uwsgi+Django+Mysql deployment project

Chapter 1, Preparations

Section 1, Create a project directory

Prepare the project code and upload the code to myproject

mkdir myproject

Section 2, install python3

cd /usr/local/
mkdir Python
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
tar -zxvf Python-3.8.0.tgz
mkdir /usr/local/Python/py3_project
cd Python-3.8.0
./configure --prefix=/usr/local/python3 --enable-shared
make & amp; & amp; make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH

Section 3, install mysql

# Online installation method, offline method will be released later
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm --nogpgcheck
yum -y install mysql-community-server
# start up
systemctl start mysqld.service
# initial password
grep "password" /var/log/mysqld.log
# Enter mysql:
mysql -uroot -p
# Modify the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'pwd';
If it doesn't work, first:
set global validate_password_policy=0;
set global validate_password_length=1;

Section 4, create mysql user and database

create database laws;
create user 'Laws'@'%' identified by 'pwd';
grant all privileges on laws.* to 'user'@'%' identified by 'pwd' with
grant option;
flush privileges;

Chapter 2, Virtual Environment

Section 1, Install the virtual environment

pip install virtualenv

Section 2, Create a new virtual environment

# Create a virtual environment named myenv under myproject
cd myproject
mkdir env
virtualenv myenv

Section 3, entering and exiting the virtual environment

source lawsenv/bin/activate
# Exit the virtual environment
deactivate

Chapter 3, install django and pymysql

Section 1, install after entering the virtual environment

# Install django, == followed by the version number
pip3 install django==3.2.16
# Install pymysql for operating mysql
pip3 install pymysql

Section 2, configuration project file __init__.py

It must be noted here that __init__.py needs to be changed to operate mysql

cd /myproject/codeproject
cd /codeproject
# Modify the configuration file for operating mysql
vi __init__.py
import pymysql
pymysql.install_as_MySQLdb()

Section 3, configuration project file settings.py

vi settings.py
DEBUG=True # When starting directly and using uwsgi, if nginx is used, change it to False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = Path(BASE_DIR, 'static')
STATICFILES_DIRS = [Path(BASE_DIR, '/static/')]

Section 4, configuration project file urls.py

? When using a domain name or ip to access directly by listening to port 80, the content displayed on the home page:

? Add to:

path('',views.index)

Start the django project in the virtual environment

python manage.py makemigrations app01
python manage.py migrate
python manage.py runserver 0.0.0.0:8000

Chapter 4, using uwsgi in a virtual environment

1, section 1, install uwsgi

pip3 install uwsgi

Section 2, start django project with uwsgi

uwsgi --http :8000 --module Laws.wsgi
#uwsgi plus hot reload command
uwsgi --http :8000 --module Laws.wsgi --py-autoreload=1

Section 3, start the project with uwsgi configuration file

cd Laws
vi uwsgi.ini
# Enter the content of the next section.
# Let go of http=0.0.0.0:8000, directly as a web server
# Release socket=0.0.0.0:8000
chmod-socket=664
# Communicate with nginx and relay requests from front-end websites. 

Section 4, content of uwsgi.ini

[uwsgi]
#Specify the project directory of django, the first layer
chdir=/myproject/mycode# here you need to write the second-level directory of the project Laws
module=Laws.wsgi
#Fill in the absolute path of the virtual environment
home=/myproject/env/myenv
master=true
processes=5
#Specify the socket protocol, run django, it can only be used in combination with nginx
socket=0.0.0.0:8000
#If you don't use nginx, just want to start an http interface by yourself, use this
#http = 0.0.0.0:8000 # directly as a web server
chmod-socket=664 #Used to communicate with nginx
# clear environment on exit
vacuum=true

Section 5, start uwsgi through the configuration file

uwsgi --ini uwsgi.ini
# When http is released, you can use the web page to enter:
The ip of the project that can be accessed on the public network: 8000/index/
myweb.com:8000/index/

Chapter 5, nginx

Section 1, collecting django static files

# Edit the settings.py configuration file of crm and write the following code
# Define the root directory of django's static resources, which is convenient for collecting resources with commands and storing them
STATIC_ROOT='/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
# Collect static files with command
python3 manage.py collectstatic

Section 2, Install Nginx

sudo yum install -y epel-release sudo yum -y update
sudo yum install -y nginx
# After successful installation,
# The default website directory is: /usr/share/nginx/html
# The default configuration file is: /etc/nginx/nginx.conf

Section 3, operating nginx

# Start Nginx:
systemctl start nginx
# Stop Nginx:
systemctl stop nginx
# Restart Nginx:
systemctl restart nginx
# View Nginx status:
systemctl status nginx
# Enable Nginx to start on boot:
systemctl enablenginx
# Disable starting Nginx on boot:
systemctl disablenginx

section 4, configure nginx

cd /etc/nginx
vi nginx.conf
# Configure according to the content on the right
server {
listen 80;
listen 443 ssl;
server_name myweb.com;
#root /usr/share/nginx/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
#error_page 500 502 503 504 /50x.html;
location = {
#Use uwsgi_pass to forward a request based on uwsgi protocol
uwsgi_pass 0.0.0.0:8000; #Same as uwsgi configuration
include /etc/nginx/uwsgi_params;
}
#Configure a url entry to tell django where to find static files
#When the request url is songwmeta.com/static/
# Just make an alias, nginx goes to /static to find js files
location /static {
alias /static/;
}
#sslon;
ssl_certificate /myproject/ssl/myweb.com_bundle.pem;
ssl_certificate_key /myproject/ssl/myweb.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
ssl_prefer_server_ciphers on;
}

section 5, upload ssl certificate

cd myproject
mkdir sll

Upload via ftp.

# Whether the startup and operation are normal
# Start Nginx:
systemctl start nginx
# View Nginx status:
systemctl status nginx

Chapter 6, configure supervisor tool, manage django background

section 1, install supervisor

# This thing can only be achieved with python2.
# download supervisor
easy_install supervisor
# Configure the supervisor configuration file and write django tasks
echo_supervisord_conf > /etc/supervisor.conf

Section 2, writing tasks to run django

vim /etc/supervisor.conf
# Write the following code at the bottom line
[program: Laws]
command=/myproject/env/myenv/bin/uwsgi --ini
/myproject/Laws/uwsgi.ini
autorestart=true
stopasgroup=true
killasgroup=true

Section 3, start the program of suopersivod

# start the server
supervisord -c /etc/supervisor.conf
# View tasks through client commands
supervisorctl -c /etc/supervisor.conf

Section 4, Supervisorctl management commands

supervisorctl -c /etc/supervisor.conf
Laws RUNNING pid 5293, uptime 0:03:03
supervisor> stop all #stop all tasks
supervisor> start all #Start s all tasks
supervisor> status Laws
# When using Superivosr for process management, uWSGI's "daemonize" cannot be enabled at the same time.
# Delete the "daemonize" item in the "uwsgi.ini" file

Chapter 7, References

https://www.cnblogs.com/zhang-zi-yi/p/10792928.html

https://theaisummer.com/uwsgi-nginx/