nginx+uwsgi+django环境部署部署
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+uwsgi+django环境部署部署相关的知识,希望对你有一定的参考价值。
首科常昊技术部
Django框架部署文档:
硬件环境:阿里云ecs服务器8核心、16GB、10Mbps
环境结构:nginx+uwsgi+django
所需软件:python-3.6.1、pip3.6、mysql-5.1.73 nginx-1.6.3、
[[email protected] src]# yum install zlib-develbzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-develtk-devel [[email protected] ~]# yum -y install opensslopenssl-devel
编译安装python3.6.1 [[email protected] leslie]# tar zxvf Python-3.6.1.tgz -C /usr/src/ [[email protected] leslie]# cd /usr/src/Python-3.6.1/ [[email protected] Python-3.6.1]# ./configure--prefix=/usr/local/python --enable-shared CFLAGS=-fPIC [[email protected] Python-3.6.1]#make&&make install
##安装完python3.6过程中自动安装了pip工具
‘‘‘
#Collecting setuptools
#Collecting pip
#Installing collected packages: setuptools,pip
#Successfully installed pip-9.0.1setuptools-28.8.0
‘‘‘
现在输入python还是python2.6
[[email protected] ~]# python Python 2.6.6 (r266:84292, Jul 23 2015,15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] onlinux2 Type "help","copyright", "credits" or "license" for moreinformation. >>>
将2.6python的程序备份
[[email protected] ~]# which python /usr/bin/python [[email protected] ~]# cd /usr/bin/ [[email protected] bin]# mv python python.bak
将3.6的python软链接到/usr/bin/python
[[email protected] bin]# ln -s/usr/local/python/bin/python3 /usr/bin/python [[email protected] bin]# python python: error while loading sharedlibraries: libpython3.6m.so.1.0: cannot open shared object file: No such fileor directory
缺少libpython3.6m.so.1.0 将python安装目录下的libpython3.6m.so.1.0链接到/usr/lib64库下
[
[email protected] bin]# ln -s/usr/local/python/lib/libpython3.6m.so.1.0 /usr/lib64/
可以使用了
[[email protected] bin]# python Python 3.6.1 (default, Oct 26 2017,11:49:35) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] onlinux Type "help","copyright", "credits" or "license" for moreinformation. >>> print("hello world") hello world >>>
现在yum无法使用了,因为yum是使用python解释器运行的,现在找不到原来版本的python了,所以无法运行了,
[[email protected] bin]# yum File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax [[email protected] bin]# which yum /usr/bin/yum
[[email protected] bin]# cat /usr/bin/yum
#!/usr/bin/python2.6 ##修改第一行为这样 import sys try: import yum except ImportError: print >> sys.stderr, """There was a problem importing one of thePython mod 。。。
yum可以使用了:
[[email protected] bin]# yum makecache Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile *base: mirrors.aliyuncs.com *epel: mirrors.aliyuncs.com *extras: mirrors.aliyuncs.com
安装 uwsgi
[[email protected] bin]#/usr/local/python/bin/pip3 install uwsgi Collecting uwsgi Downloadinghttp://mirrors.aliyun.com/pypi/packages/bb/0a/45e5aa80dc135889594bb371c082d20fb7ee7303b174874c996888cc8511/uwsgi-2.0.15.tar.gz(795kB) 100% |████████████████████████████████| 798kB 58.5MB/s Installing collected packages: uwsgi Running setup.py install for uwsgi ... done Successfully installed uwsgi-2.0.15
验证安装:
[[email protected] bin]#/usr/local/python/bin/uwsgi --version 2.0.15
安装django
[[email protected] ~]# /usr/local/python/bin/pip3install django Collecting django Downloadinghttp://mirrors.aliyun.com/pypi/packages/82/33/f9d2871f3aed5062661711bf91b3ebb03daa52cc0e1c37925f3e0c4508c5/Django-1.11.6-py2.py3-none-any.whl(6.9MB) 100% |████████████████████████████████| 7.0MB 68.8MB/s Collecting pytz (from django) Downloadinghttp://mirrors.aliyun.com/pypi/packages/55/62/e7cd0e15b76062d298413f14bb4ec3cd8568a22d274427f9c3c7286969f4/pytz-2017.2-py2.py3-none-any.whl(484kB) 100% |████████████████████████████████| 491kB 67.8MB/s Installing collected packages: pytz, django Successfully installed django-1.11.6pytz-2017.2
验证安装:
[
[email protected] ~]# python Python 3.6.1 (default, Oct 26 2017,11:49:35) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux Type "help","copyright", "credits" or "license" for moreinformation. >>> import django
>>> django.VERSION (1, 11, 6, ‘final‘, 0)
输入url:8002验证。(提示服务器ip地址加入到允许访问的列表内)
[[email protected] demosite]# vim demosite/settings.py #修改此文件
[[email protected] demosite]# python manage.pyrunserver 0.0.0.0:8002
再次运行,访问成功:
部署项目:
将项目上传到服务器:
移动到项目/opt下面,并在/opt下面新建文件夹script
[[email protected] opt]# ls gitlab online_edit_script script
在script目录里新建uwsgi.ini配置文件
[[email protected] opt]# cd script/ [[email protected] script]# ls uwsgi.ini [[email protected] script]# vim uwsgi.ini
#uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/opt/online_edit_script/
# 指定项目的application
module=online_edit_script.wsgi:application
# 指定sock的文件路径
socket=/opt/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/opt//script/uwsgi.pid
# 指定IP端口
http=127.0.0.1:9001
# 指定静态文件
static-map=/static=/opt/online_edit_script/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/script/uwsgi.log
配置nginx:
服务器上nginx已经部署了,直接修改配置文件
server { server_name www.leslie.com; location /{ include uwsgi_params; ##加载uwsgi支持模块 uwsgi_connect_timeout 30; ##超时时间 uwsgi_pass unix:/opt /script/uwsgi.sock; #转到uwsgi这个进程 } location/static/ { ##静态文件nginx自己处理 alias/opt/online_edit_script/static/; indexindex.html index.htm; } }
检测语法:
[[email protected] script]# /application/nginx/sbin/nginx -t nginx: the configuration file/application/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file/application/nginx-1.6.3//conf/nginx.conf test is successful
安装mysql:
yum -y install mysql mysql-devel mysql-server mysql-client
pip3 install mysqlclient
修改配置文件将域名加入到允许访问的列表里面。
[[email protected] ~]# vim /opt/online_edit_script/online_edit_script/settings.py
初始化uwsgi
Uwsgi –ini /opt/script/uwsgi.ini
会生成一个进程监听本地的9001端口
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 19214/uwsgi
重启nginx:
kill all nginx /usr/local/nginx/sbin/nginx
访问测试:
OK!
本文出自 “向往技术的小白” 博客,请务必保留此出处http://lesliecheung.blog.51cto.com/12622169/1976438
以上是关于nginx+uwsgi+django环境部署部署的主要内容,如果未能解决你的问题,请参考以下文章
Django + Uwsgi + Nginx 实现生产环境部署
Django + Uwsgi + Nginx 实现生产环境部署
Django + Uwsgi + Nginx 的生产环境部署