设为首页 收藏本站
查看: 1022|回复: 0

[经验分享] nginx+uwsgi+django环境部署部署

[复制链接]
发表于 2018-11-14 06:47:29 | 显示全部楼层 |阅读模式
  首科常昊技术部
  Django框架部署文档:
  硬件环境:阿里云ecs服务器8核心、16GB、10Mbps
  环境结构:nginx+uwsgi+django
  所需软件:python-3.6.1、pip3.6、mysql-5.1.73 nginx-1.6.3、
[root@shouke src]# yum install zlib-develbzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-develtk-devel  
[root@shouke ~]# yum -y install opensslopenssl-devel
编译安装python3.6.1  
[root@shouke leslie]# tar zxvf Python-3.6.1.tgz -C /usr/src/
  
[root@shouke leslie]# cd /usr/src/Python-3.6.1/
  
[root@shouke Python-3.6.1]# ./configure--prefix=/usr/local/python --enable-shared CFLAGS=-fPIC
  
[root@shouke 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
[root@shouke ~]# 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的程序备份
[root@shouke ~]# which python  
/usr/bin/python
  
[root@shouke ~]# cd /usr/bin/
  
[root@shouke bin]# mv python python.bak
  将3.6的python软链接到/usr/bin/python
[root@shouke bin]# ln -s/usr/local/python/bin/python3 /usr/bin/python  
[root@shouke 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库下
  [
root@shouke bin]# ln -s/usr/local/python/lib/libpython3.6m.so.1.0 /usr/lib64/  可以使用了
[root@shouke 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了,所以无法运行了,
[root@shouke bin]# yum  
File "/usr/bin/yum", line 30
  
   except KeyboardInterrupt, e:
  
                            ^
  
SyntaxError: invalid syntax
  

  

  
[root@shouke bin]# which yum
  
/usr/bin/yum
  [root@shouke 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可以使用了:
[root@shouke 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
[root@shouke 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
  验证安装:
[root@shouke bin]#/usr/local/python/bin/uwsgi --version  
2.0.15
  安装django
[root@shouke ~]# /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
  验证安装:
  [
root@shouke ~]# 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地址加入到允许访问的列表内)

  
[root@shouke demosite]# vim demosite/settings.py   #修改此文件

[root@shouke demosite]# python manage.pyrunserver 0.0.0.0:8002  再次运行,访问成功:

  部署项目:
  将项目上传到服务器:

  移动到项目/opt下面,并在/opt下面新建文件夹script
[root@shouke opt]# ls  
gitlab online_edit_script  script
  在script目录里新建uwsgi.ini配置文件
[root@shouke opt]# cd script/  
[root@shouke script]# ls
  
uwsgi.ini
  
[root@shouke 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;
  
         }
  
}
  
  检测语法:
[root@shouke 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-clientpip3 install mysqlclient  修改配置文件将域名加入到允许访问的列表里面。
[root@shouke ~]# 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!



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-634706-1-1.html 上篇帖子: nginx增加编译模块 下篇帖子: nginx常见问题/Tomcat服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表