Django project deployment (two)

The next task:

1. Install uwsgi web server to configure django
2. Install nginx web server to configure uwsgi
3. Run the project
4. The database is connected to other computers. Install mysql service in your own centos system.
5. Use Navicat (windows) software to remotely operate mysql in centos to create database files...

1. Start configuring uwsgi after Django is running normally

Insert picture description here
Install uwsgi

pip install uwsgi
#给uwsgi建立软链接,方便使用
ln -s /usr/local/python3/bin/uwsgi  /usr/bin/uwsgi
ln -s /data/env/pyweb/bin/uwsgi  /usr/bin/uwsgi

Insert picture description here
The website project path is /data/wwwroot/cms/, create a cms.xml file in the project root directory, and enter the following content:
Insert picture description here
Insert picture description here

Example demo:

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

Start uwsgi

uwsgi -x cms.xml
ps -ef | grep uwsgi
kill -9 2886  #停止进程的话

Insert picture description here

Two, install NGINX

#使用yum安装nginx需要包括Nginx的库,安装Nginx的库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
systemctl start nginx.service

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --list-ports
systemctl restart firewalld.service

After installation, you can check the location of nginx installation through which nginx and whereis nginx find
. The configuration file is /etc/nginx/conf.d/default.conf
. The default page storage location is /usr/share/nginx/html
Insert picture description here
Insert picture description here

Briefly explain the relationship between NGINX configuration files and the role of memory parameters in the conf file.

A server in the /etc/nginx/nginx.conf
Insert picture description here
Insert picture description here
/etc/nginx/conf.d/default.conf
configuration file corresponds to the configuration of a project.
1. The servername specifies the access domain name of the current project www.django.cn
2 , Root: the root path /data/wwrot/cms/ where the project is located. The path of www.django.cn domain name binding is /data/wwrot/cms/

For example, root /data/wwrot/cms/a/. When accessing www.django.cn/test.html, the actual access path is test.html under /data/wwrot/cms/a/

3. The location in the configuration file, if it matches the access path, there can be multiple locations, mainly to match the path. There is a principle of priority.
If there is a / in the access path, it will be matched and enter the corresponding path. root /usr/share/nginx/html is the entry path. index is the file accessed by default.
Insert picture description here
Similarly, if the path contains /50x.html, go to the following location
Insert picture description here

Run the project under django through the nginx web server

Back up the default.conf file in /usr/local/nginx/conf.d to prevent accidents. cp default.conf default.conf.bak
and then open default.conf, delete the original content, and directly add the following demo content:

server {
    
    
        listen       80;
        server_name  www.django.cn(绑定的域名 如果没有localhost);
        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; #项目路径
           
        }
#192.168.1.186/admin/login   192.168.1.186/static/img/1.jpg
        location /static/ {
    
    
        alias /data/wwwroot/mysite/static/; #静态资源路径
        }
    }
上面的 include uwsgi_params;   等价写法是 include ./uwsgi_params 。注意这个是在default.conf里的内容,而且conf.d下也没有uwsgi_params文件。
当加载nginx.conf 他会引用default.conf的内容。因为nginx.conf有句包含conf语法include /etc/nginx/conf.d/*.conf 。  他这种写法,所以是uwsgi_params放在与nginx.conf同级的目录文件夹下。

uwsgi_params模块是与uwsgi对接的。 类似于fastcgi_params是与php-fpm对接的,
nginx服务下面有一个文件uwsgi-params 文件才能保证nginx和mwsgi服务进行为联;
类似的fastcgi_params文件才能保证nginx和php-fpm服务对接关联。

Insert picture description here
Insert picture description here
The content of default.conf:
Insert picture description here
Insert picture description here

Start the project at this time

systemctl restart nginx.service
cd /data/wwwroot/cms
uswgi -x cms.xml

firewall-cmd --zone=public --add-port=8997/tcp --permanent
systemctl restart firewalld.service
firewall-cmd --list-ports

Insert picture description here
The browser visits 192.168.1.186/admin/login. (In the previous section, we used the port 192.168.1.186:8000 to access the system.)
Insert picture description here
Check the NGINX error log /var/log/nginx/error.log
Insert picture description here
Repair reference: https://www.cnblogs.com/williamjie/p/ 9604594.html
belongs to the problem of operation authority and needs to close the seLinux service. /etc/sysconfig/selinux is modified to SELINUX=disable
Insert picture description here

summary:

At this time, the Linux+Nginx+Uwsgi+Python3+mysql (public network) project has been completed.

1)启动虚拟环境source activate
2) 先通过django 自带的web服务器来运行项目
3) 安装uwsgi
4 ) uwsgi的配置文件
5) 安装nginx
6) nginx的配置
7)启动uwsgi 和nginx
8)观察nginx的错误日志和uwsgi的log文件和客户(浏览器).最直接的服务就是nginx,先查看nginx错误日志。再观察uwsgi的错误
9 ) selinux安全服务没有关闭。 安装uwsgi的问题,创建软连接

END

Guess you like

Origin blog.csdn.net/Nightwish5/article/details/111552534