这期内容当中小编将会给大家带来有关Linux系统怎样部署Django项目,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
Linux系统中部署Django项目
1.确定已经安装了2.7版本的Python; 2.安装python-devel yum install python-devel 3.安装uwsgi pip install uwsgi
1.新建一个index.py;
# index.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"1234
2.uwsgi –http :8000 –wsgi-file index.py 浏览器访问8000端口看是否有hello world输出 注意:确保8000端口能被外网访问
$ cd /var/www/ $ django-admin startproject mysite $ cd mysite $ python manage.py runserver 0.0.0.0:8000 浏览器访问8000端口看是否有hello world输出
uwsgi --http :8000 --chdir=/var/www/mysite --module mysite.wsgi`
or
`uwsgi --http :8008 --chdir /var/www/mysite --wsgi-file weixin/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192
在浏览器中访问8000端口,看能否正常访问django网站。
参数说明:
# http : 协议类型和端口号
# processes : 开启的进程数量
# workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
# chdir : 指定运行目录(chdir to specified directory before apps loading)
# wsgi-file : 载入wsgi-file(load .wsgi file)
# stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
# threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
# master : 允许主进程存在(enable master process)
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)12345678910111213
在/etc/nginx/conf.d/下创建一个针对mysite项目的配置文件,详细如下:
# /etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8000; # for a web port socket
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django 的static和 media目录
# 如果没有static或media目录,你需要先创建
location /media {
alias /var/www/mysite/media;
}
location /static {
alias /var/www/mysite/static;
}
# 将所有非静态文件的请求转给django server处理,这里的django server用的是uwsgi。
location / {
uwsgi_pass django;
include /var/www/mysite/uwsgi_params;
}
}
#你可以从/etc/nginx/uwsgi_params复制一个拷贝到/var/www/mysite/uwsgi_params。
$ cp /etc/nginx/uwsgi_params /var/www/mysite/1234567891011121314151617181920212223242526272829303132333435
需要补充说明的是,在/etc/nginx/nginx.conf文件中,在最后一行的配置是include /etc/nginx/conf.d/*.conf,也就是说,/etc/nginx/conf.d/mysite_nginx.conf是会被包含在/etc/nginx/nginx.conf中的。
/etc/init.d/nginx restart 通过浏览器访问80端口,你发现了什么?502 Bad Gateway?是不是?想一想,这是为什么呢?原因是你访问80端口时,请求的资源不是static,也不是media,这个时候Nginx就把请求转给upstream django,upstream的网关配置的127.0.0.1:8000,而127.0.0.1:8000是要靠uwsgi启动的,所以报了一个502 Bad Gateway。你,明白了吗?
注:CentOS 7启动服务的命令是systemctl restart nginx.service
执行下面一个命令,启动uwsgi。 uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi 重启Nginx服务/etc/init.d/nginx restart,再次通过浏览器访问80端口试试看。是不是成功了?
注:CentOS 7启动服务的命令是systemctl restart nginx.service
创建一个mysite_uwsgi.ini文件,内容如下:
[uwsgi]
socket=:8000
chdir = /var/www/mysite
#wsgi-file = mysite/wsgi.py
module=mysite.wsgi:application
processes = 10
threads = 2
#django执行命令uwsgi --ini mysite_uwsgi.ini即可运行如何以Emperor模式运行?什么是Emperor模式?,官网说的很清楚,如下:uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.Whenever a config file is amended, the emperor will automatically restart the vassal.按下面的步骤操作,即可以Emperor模式运行uwsgi: \1. create a directory for the vassals sudo mkdir /etc/uwsgi sudo mkdir /etc/uwsgi/vassals \2. symlink from the default config directory to your config file sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/ \3. run the emperor uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx如何创建uwsgi服务?在Linux中,一个服务其实就是一个shell脚本。在CenOS6中,服务脚本一般都在/etc/init.d/目录下。 首先我们在/etc/initd/目录下创建一个uwsgi文件,文件内容如下: #!/bin/sh
#
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $syslog $remote_fs
# Should-Start: $time ypbind smtp
# Required-Stop: $syslog $remote_fs
# Should-Stop: ypbind smtp
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Check for missing binaries (stale symlinks should not happen)
UWSGI_BIN="/usr/local/bin/uwsgi"
UWSGI_EMPEROR_MODE=true
UWSGI_VASSALS="/etc/uwsgi/vassals/"
UWSGI_OPTIONS="--uid nginx --gid nginx --logto /var/log/uwsgi/uwsgi.log"
lockfile=/var/lock/subsys/uwsgi
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then
UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS"
fi
case "$1" in
start)
echo "Starting uWSGI ... "
daemon $UWSGI_BIN $UWSGI_OPTIONS &
;;
stop)
echo "Shutting down uWSGI ... "
killproc $UWSGI_BIN
;;
restart)
$0 stop
$0 start
;;
status)
echo -n "Checking for service uWSGI "
status $UWSGI_BIN
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 012345678910111213141516171819202122232425262728293031323334353637383940414243444546然后,我们可以使用此脚本来管理uwsgi,如下:/etc/init.d/uwsgi start /etc/init.d/uwsgi stop /etc/init.d/uwsgi restart /etc/init.d/uwsgi status需要注意的是,日志文件夹的所属权应该归配置文件中指定的用户nginx $ chown nginx.nginx /var/log/uwsgi -R如何设置开机起动uwsgi?把启动uwsgi的命令添加到“/etc/rc.local”文件中即可。多站点部署问题 #Simple HTTP server
server {
listen 80;
root /usr/share/nginx/www;
server_name host1.example.com;
}
#Django server
server {
listen 80;
server_name host2.example.com;
#...upstream config...
}
上述就是小编为大家分享的Linux系统怎样部署Django项目了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。