首页 » Python » python3 中级篇 » 正文

Python3_部署上线 uwsgi+nginx+django

Python3安装

编译前 先安装:

yum install sqlite-devel -y

不提供详细安装记录,网上随便找找多得很

二、项目:

mark

2.1 安装和配置uwsgi

安装:

pip3 install uwsgi

配置:

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/data/myapp
# 指定项目的application
#wsgi-file=/data/myapp/Myapp/wsgi.py
module=Myapp.wsgi:application
# 指定sock的文件路径
socket=/data/myapp/uwsgi.sock
# 进程个数
workers=6
pidfile=/data/myapp/uwsgi.pid
# 指定IP端口
http=0.0.0.0:888
# 指定静态文件
static-map=/static=/data/myapp/mydjangoapp/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socketpid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=2096
# 设置日志目录
daemonize=/data/myapp/uwsgi.log

2.2、启动uwsgi:

uwsgi在哪个目录启动,就会在哪个目录生成uwsgi.piduswgi.log文件。

启动:uwsgi --ini uwsgi.ini

停止:uwsgi --stop uwsgi.pid

重启:uwsgi --reload uwsgi.pid

强制停止:killall -9 uwsgi

这里我们启动uwsgi服务,可以通过ps -ef | grep uwsgi看到已经有四个uwsgi服务启动。
[root@localhost uwsgi]# vim uwsgi.ini
[root@localhost uwsgi]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /data/myapp/Mydjangoapp/static

三、安装nginx:

yum -y install nginx

四、Django + Uwsgi + Nginx:

cd /etc/nginx/conf.d/

vim myapp.conf

server {
        listen 80; # 我要监听那个端口
        server_name 192.168.171.173; # 你访问的路径前面的url名称
        access_log /var/log/nginx/access.log main; # Nginx日志配置
        charset utf-8; # Nginx编码
        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型

#       error_page 404 /404.html; # 错误页面
#       error_page 500 502 503 504 /50x.html; # 错误页面

# 指定项目路径uwsgi
        location / { # 这个location就和咱们Djangourl(r'^admin/', admin.site.urls),
                include    uwsgi_params;        # 导入一个Nginx模块他是用来和uWSGI进行通讯的
#               uwsgi_pass  unix:/data/uwsgi/uwsgi.sock;        # 指定uwsgisock文件所有动态请求就会直接丢给他
                uwsgi_pass  127.0.0.1:888;      # 指定uwsgisock文件所有动态请求就会直接丢给他
                uwsgi_param UWSGI_CHDIR  /data/myapp;
                uwsgi_param UWSGI_SCRIPT django_wsgi;
                uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
#               proxy_pass  http://django;

}

# 指定静态文件路径
        location /static {
        alias /data/myapp/mydjangoapp/static;
#       index index.html index.htm;
}

}

当然线上的我们必须通过域名来访问:

upstream django {
		server 192.168.171.173:888;
}

server {
	listen 80; # 我要监听那个端口
	server_name django.zhdya.com; # 你访问的路径前面的url名称 
	access_log /var/log/nginx/access.log main; # Nginx日志配置
	charset utf-8; # Nginx编码
	gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型

#	error_page 404 /404.html; # 错误页面
#	error_page 500 502 503 504 /50x.html; # 错误页面

# 指定项目路径uwsgi
	location / { # 这个location就和咱们Djangourl(r'^admin/', admin.site.urls),
	        include    uwsgi_params;	# 导入一个Nginx模块他是用来和uWSGI进行通讯的
            uwsgi_pass  unix:/data/uwsgi/uwsgi.sock;	# 指定uwsgisock文件所有动态请求就会直接丢给他
#           uwsgi_pass  127.0.0.1:888;	# 指定uwsgisock文件所有动态请求就会直接丢给他
            uwsgi_param UWSGI_CHDIR  /data/myapp;
           	uwsgi_param UWSGI_SCRIPT django_wsgi;
			uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
			proxy_pass  http://django;
		
}

# 指定静态文件路径
	location /static {
	alias /data/myapp/mydjangoapp/static;
#	index index.html index.htm;
}

}

直接访问 http://django.zhdya.com 即可!

先看故障:

解决No module named _sqlite3的问题:

1 安装 sqlite-devel

yum install sqlite-devel

2 重新编译python

cd /usr/local/python3.6.4
./configure
make
make install

五、django

pip3 install django

然后手贱的事情就这么出现了:

踩坑之ModuleNotFoundError: No module named ‘mydjangoapp’

如下报错:

detected number of CPU cores: 1
current working directory: /root
detected binary path: /usr/local/bin/uwsgi
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /data/myapp/Myapp
your processes number limit is 3806
your memory page size is 4096 bytes
detected max file descriptor number: 1024
!!! no /etc/mime.types file found !!!
lock engine: pthread robust mutexes
thunder lock: enabled
uWSGI http bound on 0.0.0.0:888 fd 3
uwsgi socket 0 inherited UNIX address /data/uwsgi/uwsgi.sock fd 6
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.6.1 (default, Jan  8 2019, 08:08:02)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Python main interpreter initialized at 0x16e7f70
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 567784 bytes (554 KB) for 6 cores
*** Operational MODE: preforking ***
ModuleNotFoundError: No module named 'mydjangoapp'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***

搞了整整一天才发现错误原来是自己手贱 在Copy项目到/data 目录中 自己手动改了Mydjangoapp 为mydjangoapp 结果就这么各种查资料。有的是什么虚拟目录,有的是模块报错,直到发现view.py中from Mydjangoapp 才晓得原来自己手贱,,,重命名了主目录!!!

来个效果图吧:

mark

mark

赞 (2)

本文共 2 个回复

  • lawliets 2019/01/11 21:31

    这个跟CMDB有点像,是不是都是资产管理系统

发表评论