ubuntu+uwsgi+nginx+django 手记,uwsginginx


nginx

  • 安装nginx
apt-get install nginx
  • 启动与停止
#启动nginx服务
service nginx start
#查看nginx运行状态
ps -ef | grep nginx
#停止nginx服务
service nginx stop

测试,浏览器输入ip地址,显示如下:

  • 项目nginx配置

1.在工程下新建文件nginx.conf

  • nginx.conf:
# the upstream component nginx needs to connect to
upstream django {
server unix:///your/project/path/mysite.sock; # for a file socket
#server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name  your_IP_address; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /your/project/path/media;  # 指向django的media目录
}

location /static {
    alias /your/project/path/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}

2.将工程下nginx.conf文件建立软连接到/etc/nginx/conf.d/下

ln -s /your/project/path/nginx.conf /etc/nginx/conf.d/

3.拉取所有需要的static file 到同一个目录

#在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#运行命令
python manage.py collectstatic

nginx相关文件目录

#Nginx 目录、文件 
web目录:/root /var/www/html 
安装目录:/etc/nginx 
服务主机配置文件:/etc/nginx/sites-available/default
日志:/var/log/nginx/error.log
#Nginx 脚本相关操作 
开启:sudo /etc/init.d/nginx start 
重启:sudo /etc/init.d/nginx restart 
停止:sudo /etc/init.d/nginx stop 
状态:sudo /etc/init.d/nginx status

/etc/nginx/nginx.conf 配置文件详解
https://www.cnblogs.com/phpdragon/p/3248373.html

uwsgi

  • 项目配置文件uwsgi.ini
# uwsgi.ini file
    [uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /your/project/PATH
# Django's wsgi file
module          = yourprojectNAME.wsgi

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 3
#buffer-size = 65536
# the socket (use the full path to be safe
#socket          = /your/project/PATH/mysite.sock
#socket          = 127.0.0.1:8000
http          = your machine's IP:8000
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
#    logto = /tmp/mylog.log
# 存储pid进程
pidfile		=uwsgi0.pid
# 存储uwsgi状态
stats		=uwsgi.status
#后台运行,存储log
daemonize 		=uwsgi.log

  • 启动
uwsgi -i uwsgi.ini
  • 停止
#查看进程号
cat uwsgi.pid
#停止进程
uwsgi --stop uwsgi.pid
kill pid
  • 服务器开机自启动
#编辑文件/etc/rc.local, 添加下面内容到这行代码之前exit 0:
/usr/local/bin/uwsgi --ini /your/project/path/uwsgi.ini

相关内容

    暂无相关文章