目录
二: 部署uwsgi
打通uWSGI和Python
打通uWSGI和Django
三: 配置niginx
nginx简单的操作命令
四:Nginx + uwsgi
五:supervisor管理uwsgi(可选项)
一: 安装需要的包
1:
# sudo apt-get install python-dev nginx
(提示: 如果执行完显示不能安装,请更新aot-get, #sudo apt-get update)
2:
# sudo apt-get install uwsgi
-
二: 部署uwsgi
打通uWSGI和Python
在项目根目录创建test.py文件,内容如下
1
2
3
4
5
6def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2 # return [b"Hello World"] # python3
运行uWSGI(表示使用http协议,并使用8000端口,加载指定文件test.py)
1uwsgi --http :8000 --wsgi-file test.py
打开浏览器,输入
1127.0.0.1:8000
若显示’Hello World’则表示运行正常,说明以下三个环节是相通的
web client <-> uWSGI <-> Python
打通uWSGI和Django
在项目根目录创建文件blog_uwsgi.ini,并写入以下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28# blog_uwsgi.ini file [uwsgi] # 使用nginx连接时使用 # socket = 0:8001 # 直接做web服务器使用 http = 0:8080 # the base directory (full path) chdir = /var/www/Blog # Django's wsgi file wsgi-file = /var/www/Blog/Blog/wsgi.py module = Blog.wsgi # master # 主进程 master = true # 多进程&多线程 processes = 6 threads = 2 # .sock文件目录需与Nginx文件内的配置相同(暂时可不配置) #socket = /var/www/Blog/Blog.sock #chmod-socket = 666 # 以守护进程的方式启动 vacuum = true # 存储pid进程 pidfile=uwsgi.pid # 存储log日志 daemonize=uwsgi.log ~
启动uWSGI服务
uwsgi --ini my_uwsgi.ini
终端显示以下内容即代表开启成功
1[uWSGI] getting INI configuration from my_uwsgi.ini
打开浏览器,地址栏输入以下地址
<YOUR_SERVER_IP>:8080
-
三: 配置niginx
nginx文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17root@ubuntu:/etc/nginx/conf.d# cat blog_nginx.conf server { listen 80; //80默认监听端口,建议不要改 server_name 65.49.230.245; //服务器ip或域名 location /static { alias /var/www/Blog/static; //静态资源 } location / { proxy_pass http://localhost:8080; //反向代理 include /etc/nginx/uwsgi_params; } }
/etc/nginx/nginx.conf下有如下内容
1
2include /etc/nginx/conf.d/*.conf; //简单直接(适合新手) include /etc/nginx/sites-enabled/*; //如果有多个nginx文件则可考虑
则nginx文件配置存在两种方式
第一种include /etc/nginx/conf.d/*.conf;
将blog_nignx.conf直接放在/etc/nginx/conf.d目录下(简单)
第二种include /etc/nginx/sites-enabled/*;
将blog_nignx.conf直接放在/etc/nginx/sites-available目录下
然后建立软链接,nginx启动的都是软连接的文件配置
1
2sudo ln -s /etc/nginx/sites-available/blog_nignx.conf /etc/nginx/sites-enabled/blog_nignx.conf
其实都是读nginx.conf。第二种include sites-enabled/*.conf。sites-available是为了方便保存不用的配置。启用的时候nginx文件只要ln软链接一下就可以。
sites-available 和 sites-enabled:
sites-available目录是存放可用的内容,但不起作用,只有用ln 连到sites-enabled目录才可以起作用。sites-enabled目录存放真正起作用的配置文件,存放一些指向sites-available目录的符号链接。所以apache上配置了多个虚拟主机,每个虚拟主机的配置都放在sites-available下,那么对于虚拟主机的停用和启用就非常方便。当sites-enabled下建立一个指向某个虚拟主机配置文件的连接时,就启用了它。如果要关闭某个虚拟主机的话,只需要删除相应的符号链接即可,不用去改配置文件。
奇葩问题:
之前使用第二种的时候, 软连接过后,重启nginx一直不行, 试一下将blog_nignx.conf复制到/etc/nginx/sites-enabled/, 然后就可以了
-
然后就可以直接通过localhost/index.htm来访问8080端口的项目了.
nginx简单的操作命令
1
2
3
4sudo service nginx start #启动 sudo service nginx stop #停止 sudo service nginx restart #重新启动 sudo nginx -s reload #重新加载配置
-
四:Nginx + uwsgi
nginx文件
root@ubuntu:/etc/nginx/conf.d# cat blog_nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22upstream django { # server 127.0.0.1:8080; server unix:///var/www/Blog/Blog.sock; } server { listen 80; server_name www.victor-yux.com; location /static { alias /var/www/Blog/static; } location / { uwsgi_pass django; # 允许js跨域请求 add_header Access-Control-Allow-Origin *; allow all; include /etc/nginx/uwsgi_params; } }
uwsgi文件
root@ubuntu:/var/www/Blog# cat Blog_uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25[uwsgi] # 使用nginx连接时使用 # socket = 0:8001 # 直接做web服务器使用 #http = 0:8080 # the base directory (full path) chdir = /var/www/Blog # Django's wsgi file wsgi-file = /var/www/Blog/Blog/wsgi.py module = Blog.wsgi # master # 主进程 master = true # 多进程&多线程 processes = 6 threads = 2 # .sock文件目录需与Nginx文件内的配置相同 socket = /var/www/Blog/Blog.sock chmod-socket = 666 # 以守护进程的方式启动 vacuum = true # 存储pid进程 pidfile=uwsgi.pid # 存储log日志 daemonize=uwsgi.log
将nginx和uwsgi启动后, 访问www.victor-yux.com,
-
五:supervisor管理uwsgi(可选项)
# sudo apt-getinstall supervisor
生成supervisor默认配置文件, 一般放在/etc/supervisord.conf路径中:
# echo_supervisord_conf> /etc/supervisord.conf
1:打开/etc/supervisord.conf在最底部添加(每一行前面不要有空格,防止报错):
1
2
3
4
5
6
7[program:Blog] command=/usr/local/bin/uwsgi --ini /var/www/Blog/Blog_uwsgi.ini directory=/var/www/Blog startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
2:
启动 supervisor
# sudo supervisord -c /etc/supervisord.conf
重启anytum项目:
# sudo supervisord -c /etc/supervisord.conf restart Blog
启动,停止,或重启 supervisor 管理的某个程序或所有程序:
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
链接:
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#concept uwsgi 和nginx部署官方文档
http://www.jianshu.com/p/e6ff4a28ab5a基于nginx和uWSGI在Ubuntu上部署Django
http://www.ziqiangxuetang.com/django/django-nginx-deploy.html django 自强学堂 部署环境
最后
以上就是害羞羽毛最近收集整理的关于ubuntu14 nginx+uwsgi配置的全部内容,更多相关ubuntu14内容请搜索靠谱客的其他文章。
发表评论 取消回复