Linux 安装并部署python+django+uwsgi+nginx总结
1. python和django的环境搭建
(1)下载anaconda3并安装
wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda3-2.3.0-Linux-x86_64.sh
sh Anaconda3-2.3.0-Linux-x86_64.sh
一路enter键,然后提示是否加入到环境变量时,输入yes即可。
(2)安装django
直接pip install django
安装成功之后就可以新建项目
django-admin startproject demosite
cd demosite
python manage.py startapp blog
python manage.py migrate (要执行这个命令,让django生成可运行的app,否则后面使用uwsgi会报错)
(3)运行django
python manage.py runserver
curl 127.0.0.1:8000进行如果可以正常访问,就说明django安装成功。
2. 安装uwsgi
(1)centOS
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
pip install uwsgi
uwsgi --version #查看 uwsgi 版本
(2)test.py
然后,Run uWSGI:
uwsgi --http :8000 --wsgi-file test.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2 #return [b"Hello World"] # python3
(3)ubuntu可以能会出现错误:
如果出现错误,!!! no internal routing support, rebuild with pcre support !!!
sudo apt-get install libpcre3 libpcre3-dev
sudo pip uninstall uwsgi
sudo apt-get remove uwsgi
sudo pip install uwsgi
(4)测试
1) 打开下面url,浏览器上应该显示hello world
curl http://127.0.0.1:8000 如果安装httpie模块的话使用http http://127.0.0.1:8000
如果显示正确是Hello World,说明上面的环节是畅通的
2) 测试django
默认使用django新建工程会在app下面生成一个wsgi.py的文件
uwsgi --http :8000 --wsgi-file wsgi.py 直接这样也会报错
uwsgi --http :8000 --wsgi-file appname/wsgi.py
打开浏览器输入http://127.0.0.1:8000 如果现实正确说明web client <-->uwsgi <---> django是畅通的
3. 安装配置nginx
(1)安装
wget http://nginx.org/download/nginx-1.9.5.tar.gz
tar xf nginx-1.9.5.tar.gz
cd nginx-1.9.5
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module
make && make install
或者参考
http://cantgis.blog.51cto.com/5788192/1540004
(2)配置文件
vi /usr/local/nginx/conf/nginx.conf
一般来说加入个server就OK了
参考配置如下
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 65535; } 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 { listen 8099; server_name 10.117.52.157; ##对外访问的IP和端口号 access_log /tmp/cms/access.log; error_log /tmp/cms/error.log; #charset koi8-r; #access_log logs/host.access.log main; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8088; uwsgi_read_timeout 300; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #error_page 500 502 503 504 /registration/500.html; #location = /registration/500.html { # root html; #} location /static/ { alias /root/cms/cms/static/; index index.html index.htm; } location /media/ { alias /root/cms/cms/media/; } } }
(3)运行and 重启
/usr/local/nginx/sbin/nginx
启动: nginx start
重启: nginx -s reload
4. 使用uwsgi的配置文件运行django
在确保nginx运行之后,就可以通过uwsgi来运行django了。nginx 在最外层接收请求,静态的自己处理,动态的通过 socket端口交给uwsgi来处理。
配置文件内容如下
[uwsgi]
socket=:8088 #要和nginx对应的IP和端口号一致
chdir=/root/cms/cms #APP的目录
module=cms.wsgi #wsgi.py文件位置
touch-reload=/root/cms/cms/reload #重启只要输入命令touch reload文件即可
processes=4
threads=2
daemonize=/tmp/cms/wsgi.log #日志文件位置
放在APP的上一级目录
直接运行uwsgi --ini uwsgi.ini 即可