django+uwsgi+nginx,经验证过

  1. 安装uwsgi
  2. 验证uwsgi是安装成功&配置
  3. uwsgi+django
  4. 安装nginx&配置
  5. uwsig+nginx+django
  6. 访问最终的工程

一、安装uwsgi:(ps:先安装yum工具)
1、yum install python3-devel 或者 yum install python3-dev /
2、yum install uwsgi /pip install uwsgi

二、验证uwsgi是安装成功&配置
1、在工程中创建一个test.py文件,写入如下内容:
def application(env, start_response):
start_response(‘200 OK’, [(‘Content-Type’,‘text/html’)])
return “Hello World”
2、在终端运行
uwsgi --http :8001 --wsgi-file test.py
3、在浏览器上输入 127.0.0.1:8001,127.0.0.1表示是在本地弄的nginx,如果是在其它服务器地址,则将127.0.0.1改成服务器地址即可
4、页面中显示 Hello World ,即uwsgi 安装是成功了
5、相关命令:
uwsgi --reload uwsgi.pid  重启
uwsgi --stop uwsgi.pid 停止
uwsgi --ini uwsgi.ini 启动
ps ef |grep uwsgi 查看uwsgi进程
6、在django项目中的 manage.py 同一 目录下,创建一个新的目录uwsgi_config,创建 uwsgi.ini,uwsgi.log,uwsgi.pid,再编辑uwsgi.ini,内容如下:

[uwsgi]
# 指定IP端口,ip为内网ip,如果是云服务器,是云服务器内网ip
# http可以直接访问,socket是让Nginx指定的
http=10.121.2.104:8087
#socket=10.121.2.104:8087
# 项目目录,和manage.py同级的目录
chdir=/var/lib/jenkins/workspace/SRM/srmAutoData
# 启动主进程,来管理其他进程
master=true
# 加载一个WSGI模块,这里加载wsgi.py这个模块
# 通常是在<django项目>/<和项目同名文件夹>/wsgi.py
# 但是如果chdir指定了项目绝对路径,只需要写<和项目同名文件夹>/wsgi.py
module=srmAutoData.wsgi:application
# 启动多少个进程,和核心数一样就行
processes=2
wsgi-file=srmAutoData/wsgi.py
file=/var/lib/jenkins/workspace/SRM/srmAutoData/srmAutoData/wsgi.py
log-maxsize=102400

# 每个进程最大的请求数
max_requests=1000
# 运行的日志,通常放在 uwsgi_config 下
daemonize=/var/lib/jenkins/workspace/SRM/srmAutoData/uwsgi_config/uwsgi.log
## 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
profiler=true
memory-report=true
enable-threads=true
logdate=true
limit-as=6048

# 如果使用的是虚拟环境, 需要指定 pythonpath
pythonpath=/home/ltops/.virtualenv/srmAutoData/bin/python3.8

# 指定pid文件,用于重启和停止,通常放在 uwsgi_config 下
pidfile=/var/lib/jenkins/workspace/SRM/srmAutoData/uwsgi_config/uwsgi.pid
# 启用线程
enable-threads=true
#设置在平滑的重启(直到接收到的请求处理完才重启)一个工作子进程中,

reload-mercy=8

三、 uwsgi+django
1、启动 uwsgi (ps:这个执行后,会自动生成uwsgi.pid,里面存的只有uwsgi的母进程号)
uwsgi --ini uwsgi.ini
2、访问 10.121.2.104:8087/homePage ,homePage是django的设定访问地址,如果正常访问,则成功
3、如果不成功,建议看一下 uwsgi.log的日志

四、安装nginx&配置:
1、下载包:wget http://nginx.org/download/nginx-1.13.7.tar.gz
2、解压包:tar -zxvf nginx-1.13.7.tar.gz
3、编译&安装;
进入解压后的nginx-1.13.7文件夹,依次进行如下操作:
./configure
make
make install
4、相关命令:
# nginx停止命令
nginx -s stop
# 启动
进入 nginx目录,xxx/nginx/sbin/nginx

./nginx
# 重启
nginx -s reload
# 查看uwsgi进程
ps ef |grep uwsgi
5、进入/nginx-1.13.7/conf/nginx.conf,添加如下内容:
注意:注意,注意,注意,必看,不然不会成功 nginx+uwsgi+django

	 - 在使用nginx+uwsgi+django时,uwsgi.ini文件中http需要改成socket
	 - nginx.conf中 uwsgi_pass值必须是与uwsgi.ini中的http/socket值一样
	 - 在外部访问时 :
	 - ip:uwsgi_pass中的ip,即:10.123.2.104
	 - 端口:等于listen的值,即:8088
#user  nobody;
worker_processes  1;
user root;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
    	#外部访问时,使用8088
        listen       8088;
#        listen       [::]:8088;  
	server_name  srmAutoData;
	charset      utf-8;
	access_log   /var/lib/jenkins/workspace/SRM/srmAutoData/nginx/logs/access.log;
	error_log    /var/lib/jenkins/workspace/SRM/srmAutoData/nginx/logs/error.log;
	client_max_body_size 75M;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	location /static {
	
	    alias /var/lib/jenkins/workspace/SRM/srmAutoData/static;
	
	}
        location / {
            #root   html;
        #    root   /var/lib/jenkins/workspace/SRM/srmAutoData;
	 #   index  index.html index.htm;

	    include /var/lib/jenkins/workspace/SRM/srmAutoData/nginx/conf/uwsgi_params;
	    uwsgi_pass 10.121.2.104:8087;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

五、django+nginx+uwsgi
1、启动nginx:nginx
2、重启uwsgi:uwsgi --reload uwsgi.pid
3、如果上面失败,则看一下 nginx目录下的 error.log查看nginx错误信息,uwsgi.log日志查看uwsgi的错误

六、访问
10.121.2.104:8088/homePage,正常访问,说明成功

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值