Linux之nginx-uwagi-django 搭建

1. uwsgi

1.1 配置文件
新建文件:touch uwsgi.ini

# uwsig使用配置文件启动
[uwsgi]
# 项目所在的根目录
chdir=/object/test_obj/django_test

# 指定项目的application
module=test_obj.wsgi:application

# 进程个数
processes = 4

# 每个进程worker数
workers=2

# 指定IP端口,web访问入口(单独使用uwsgi服务访问)
# http=X.X.X.X:8003

# 启动uwsgi的用户名和用户组
uid=root
gid=root

# 启用主进程
master=true

# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true

# 设置日志目录
daemonize=/etc/nginx/uwsgi_log.log
# uWSGI进程号存放
pidfile=%(chdir)/uwsgi_conf/uwsgi.pid

 # 指定和nginx通信的端口
socket=127.0.0.1:8001

# 设置用于uwsgi包解析的内部缓存区大小为64k。默认是4k
buffer-size=65535 

# socket 权限
chmod-socket = 664
1.2 uwsgi 命令
uwsgi --ini uwsgi.ini 启动
uwsgi --reload uwsgi.ini  重启
uwsgi --stop uwsgi.pid  关闭
pkill -9 uwsgi

2. nginx

2.1 配置文件
yum install nginx
cd /etc/nginx
vi nginx.conf

文件内容:
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
   # include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
	    server_name www.qqc-home.com;

   	    access_log /var/log/nginx/access.log;
    	charset utf-8;

        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;

        location / {
            # nginx转发动态请求到uWSGI
    	    uwsgi_pass 127.0.0.1:8001;  # 与uwsgi配置文件中的socket 一致
            include uwsgi_params;
            uwsgi_connect_timeout 20;
        }
    
        # 如果写成/static/,nginx无法找到项目静态文件路径
        location /static {
            alias /object/test_obj/django_test/static;
        }
    
        # 如果写成/media/,nginx无法找到项目媒体文件路径
        location /media {
            alias /object/test_obj/django_test/media;
        }

}

2.2 nginx 命令
# 检查配置文件
nginx -t
# 重新加载配置文件
nginx –s reload
# 启动
systemctl start nginx.service 
#查看状态
systemctl status nginx.service
# 开机自启
systemctl enable nginx.service
# 重启
systemctl restart nginx.service
# 关闭
systemctl stop nginx

注:yum命令会自动创建nginx.service文件
源码编译安装的,所以要手动创建nginx.service服务文件
vi /lib/systemd/system/nginx.service
具体的下次再研究啦~~~

查看进程
[root@iZuf6fy2kg5mx828krkhcuZ uwsgi_conf]# ps -aux|grep nginx
root      8297  0.0  0.2 120904  2112 ?        Ss   Jun29   0:00 nginx: master process /usr/sbin/nginx
root      8298  0.0  0.3 121292  3568 ?        S    Jun29   0:00 nginx: worker process
root     21872  0.0  0.0 112712   964 pts/1    R+   10:00   0:00 grep --color=auto nginx

3. 测试

http://www.qqc-home.com/redis/test

4.django项目地址

https://github.com/chaochaoge123/django_test

posted @ 2019-07-20 16:28  朝朝哥  阅读(563)  评论(0编辑  收藏  举报