Centos7.6 deploy django+nginx+uwsgi

First, you need to prepare a centos7.6 virtual machine that has been installed. This is easy to implement with vm
First agree on $ for the system user # as the system administrator


First enter the system execution # yum update -y


Then install the software management pack and the system dependent environment that may be used
Execute # yum -y groupinstall "Development tools"


Then execute
:# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel


After a long wait, I started installing python

Here is an installation source operation
: # Pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package name

Here we use the source of Tsinghua University, the domestic source, the download speed is much faster than the foreign source, can
Switch to the /usr/local directory
” # cd /usr/local


Then start downloading python
:# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
If there is an error message, install the download tool first
:# yum install wget
(The download from the official python website is very slow.... A long wait...)


After the download is complete, unzip the downloaded package
:# tar -zxvf Python-3.6.6.tgz


After decompression is complete, use ls to look at the directory
Then enter the Python 3.6 directory
: # ls
: # cd Python-3.6.6


Continue to compile and install python3 to the specified directory
: # . /configure –prefix=/usr/local/python3
Equal sign = The following path can be defined by yourself, but remember, because it will be used later


make it
:# make


Then install
:# make install


Establish a python soft connection after installation
: # ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3


Establish pip soft connection after installation
:# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip


Then check the version of python and the version of pip
: # python -v
: # pip -v
Normal display means the installation is complete


Then install virtualenv, create a virtual environment, in order to avoid conflicts between multiple projects
: # pip install virtualenv


Then establish a soft connection for the created virtualenv
: # ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv


Create env directory and site directory
: # mkdir -p /data/env
: # mkdir -p /data/wwwroot


Switch to the /data/env directory to create a virtual environment of the specified version
: # cd /data/env
: # virtualenv –python=/usr/bin/python3 pyweb


Enter /data/env/pyweb/bin to start the virtual environment
: # cd /data/env/pyweb/bin
: # source activate
If it is successful, pyweb will be displayed on the console
Such:
#####( pyweb) [root@localhost bin]#


Then install Django and uwsgi in the virtual environment
:# pip install django
django can be followed by = equal sign to specify the version to install
: # pip install uwsgi
Then establish a soft connection to uwsgi
: # ln -s /usr/localpython3/bin/uwsgi /usr/bin/uwsgi


After the establishment is complete, switch to the website directory /data/wwwroot
: # cd /data/wwwroot
Then create the django project
: # django-admin.py startproject mysite
After waiting for a period of time, the project is automatically created, and then enter the mysite project to create the app
: # cd mysite
Create app
: # python manage.py startapp blog


I started to encounter an error and reported that the sqlite database does not exist, then I changed the sqlite in the setting to mysql, and configured the database information, and then reported that the mysqlclient client version is too low, I xx, no way, solve it There are two schemes, 1. Change the mysql version, 2. Change to mysqlclient, the second one was tried but failed, so I can only change it, which is really unreliable


Create static and templates respectively to store static files and template files
: # mkdir static
: # mkdir templates
Then edit the setting.py file in mysite
: # cd mysite
Add blog app in INSTALLED_APPS
Modify ALLOWED_HOSTS=['*']
Allow all network access
Add template path os.path.join(BASE_DIR,'templates')
And add the static file configuration path at the end
STATICFILES_DIRS = (os.path.join(BASE_DIR,’static’),)
Then switch to templates to create html files
Feel free to add some code here


<! DOCTYPE html>
< html>
< head>
< meta charset=”utf-8″>
< title>My website
< /head>
< body>
:

hello! Welcome to my website!


< /body>


Then configure django's url routing
Enter the mysite directory
: # cd /data/wwwroot/mysite/mysite
Open the urls.py file
: # vi urls.py


Configuration:
from django.urls import path
from . import views
urlpatterns=[
path(”,views.index),
]


Configure views
Enter the blog directory
: # cd /data/wwwroot/mysite/blog
: # vi views.py


from django.shortcuts import render
def index(request):
return render(request,’index.html’)


Then you can start the project,
: # python manage.py runserver
Then enter the address of the terminal in the browser: 127.0.0.1::8000
Then you can see the content in html,


Then configure uwsgi
Switch to the mysite project
: # cd /data/wwwroot/mysite
Create mysite.xml file
: # vi mysite.xml


< uwsgi>
< socket>127.0.0.1:8997<!– 内部端口,自定义 –>
< chdir>/data/wwwroot/mysite/<!– 项目路径 –>
< module>mysite.wsgi
< processes>4<!– 进程数 –>
< daemonize>uwsgi.log<!– 日志文件 –>


Install nginx and configure nginx.conf file
Enter the home directory
: # cd /home
: # wget http://nginx.org/download/nginx-1.13.7.tar.gz
Unzip nginx
: # tar -zxvf nginx-1.13.7.tar.gz
Then execute the following commands in turn
: # cd nginx-1.13.7
: # . /configure
: # make
: # make install
The default path of nginx installed is /usr/local/nginx
Enter /usr/local/nginx/conf
: # cd /usr/local/nginx/conf
Then open the conf file
: # vi nginx.conf


charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8997;
uwsgi_param UWSGI_SCRIPT mysite.wsgi;
uwsgi_param UWSGI_CHDIR /data/wwwroot/mysite;
}
location /static/ {
alias /data/wwwroot/mysite/static/; #Static file directory


Enter /usr/local/nginx/sbin/ directory
:# /usr/local/nginx/sbin/
:# ./nginx -t
See the word "successful" to indicate success, and then execute
: # ./nginx
If there is no prompt, the startup is successful


Visit the project's page
Enter the Django project directory
: # cd /data/wwwroot/mysite/
: # uwsgi -x mysite.xml
If there is no error, continue to enter the directory
:# cd /usr/local/nginx/sbin/
Restart nginx to access your page
: # . /nginx -s reload
If you forget to configure everyone to visit, enter the virtual environment
: # cd /data/env/pyweb/bin
Start the virtual environment
:# source activate
View uwsgi process
: # ps -ef | grep uwsgi
Then enter the project root directory to start the xml file
uwsgi mysite.xml
Then enter the nginx directory to restart nginx
: # cd /usr/local/nginx/sbin/
Restart nginx
: # . /nginx -s reload


This is the configuration of django + nginx, a bit cumbersome, but not difficult

Recommended Posts

Centos7.6 deploy django+nginx+uwsgi
CentOS 7 deploy OpenLDAP+FreeRadius
Kickstart+PXE automatically deploy CentOS6.6
Deploy GitBook under CentOS7
CentOS 6.8 deploy zookeeper cluster
CentOS 7 deploy saltstack service
Deploy JDK+Tomcat8 under CentOS
Deploy vuepress on centos7
Centos6.8 deploy vnc service
Deploy Jenkin on centos7
CentOS7 deploy vsftp (FTP) service
CentOS 8 (2)
CentOS 7.2 deploy Node.js development environment
Deploy front-end projects using centOS 7
CentOS 7.2 deploy mail server (Postfix)
CentOS 8 (1)
CentOS7.7 deploy k8s (1 master + 2 node)
CentOS7.7 deploy k8s (3 master + 3 node + 1 client)
CentOS7.7 deploy k8s + Prometheus (1 master + 2 node)
Deploy and optimize Tomcat under Centos
CentOS 7.2 deploy website access log analyzer-Piwik
centos7 python3.7+vi
CentOS + Python3.6+
CentOS + Jenkins
Deploy Docker and configure Nginx in CentOS
1.5 Install Centos7
2019-07-09 CentOS7 installation
centos7_1708 installation
Centos 7.5 python3.6
Deploy Hadoop cluster services in actual CentOS system