搭建Python服务器

作者: Mr_Zander | 来源:发表于2017-05-02 13:37 被阅读230次

    环境

    操作系统:CentOS 7.2 64bit
    Nginx版本:1.6.3


    python

    Python版本:2.7
    uwsgi版本:2.0.15

    Paste_Image.png

    django版本:1.11

    安装

    1. 安装基础开发包
      yum groupinstall "Development tools"
      yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
    
    1. 安装pip

    2. 为了找到python-pip先安装epel扩展源 sudo yum -y install epel-release

    3. 安装pip sudo yum -y install python-pip

    4. 清楚cache sudo yum clean all

    5. 安装uwsgi
      pip install uwsgi

    6. 安装Django
      pip install django

    7. 安装Nginx

    8. 下载 wget [nginx address]

    9. 解压 tar xf [压缩包]

    10. 进入解压目录 cd [nginx 目录]

    11. ./configure --prefix=/usr/local/nginx-1.5.6 --with-http_stub_status_module --with-http_gzip_static_module make && make install

    开始

    1. 新建Django项目

    2. 新建项目django-admin.py startproject [projectname]

    3. cd [projectname]

    4. python manage.py runserver 0.0.0.0:8000

    5. 在浏览器中输入 ip:8000,检查django是否运行正常

    6. 配置Nginx
      在nginx.conf文件中新增一个server

      server {
              listen 8081;
          location / {
           include uwsgi_params;
              uwsgi_pass 127.0.0.1:8000;
          }
          }
    
    1. 配置uwsgi
    2. cd [projectName]
    3. vi [projectName].ini并填入内容
    [uwsgi]
    socket = 127.0.0.1:8000
    chdir = [projectPath]
    wsgi-file = [projectName]/wsgi.py
    processes = 4
    threads = 2
    daemonize = [输出log的文件] # 使项目后台运行并且输出日志
    
    1. 启动服务
    2. 重启nginx sudo nginx -s reload
    3. 启动uWsgi uwsgi xxx.ini
    4. 测试:在浏览器中输入 ip:8081

    遇到的问题:

    1. You may need to add u'' to ALLOWED_HOSTS.
      在[项目路径]/[项目名]下面有一个setting.py,把其中的ALLOWED_HOSTS修改为
      ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']
    2. Address already in use
    3. 清除tcp端口连接sudo fuser -k 9002/tcp
    4. 重启uwsgi uwsgi xxx.ini

    参考链接:

    [Setting up Django and your web server with uWSGI and nginx](http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
    https://docs.djangoproject.com/en/1.11/intro/tutorial01/)

    相关文章

      网友评论

        本文标题:搭建Python服务器

        本文链接:https://www.haomeiwen.com/subject/tqpctxtx.html