美文网首页
python+nginx+uwsgi的服务器搭建

python+nginx+uwsgi的服务器搭建

作者: Moshimol | 来源:发表于2017-12-13 16:28 被阅读119次

    python+nginx+uwsgi的服务器搭建

    搭建一个python的服务器,用来接收API的开发,以及进行数据爬虫相关分析
    

    阿里云的云服务器(ECS)可以选择多种操作系统,打算用它运行 Drupal 或者 WordPress ,你最好选择 Linux 系统,这篇文章的演示是基于阿里云的 CentOS 操作系统的服务器。我们在上面搭建一个 nginx + ots + oss + uwsgi,我们不过多解释什么是什么,而是着重讲流程与方法,想了解具体的细节,去搜索下吧:)这个手册是在阿里云上测试的,不过应该也适用于其它使用 CentOS 系统的服务器。

    购买服务器

    在阿里云网站进行购买服务器,选择对应的操作系统如下图所示

    服务器相关配置

    远程控制你的服务器

    远程控制 Linux 类型的系统的服务器,比如 CentOS 系统的服务器,一般不像 Windows 服务器那样,使用图形界面的远程控制。我们需要使用命令行工具,远程连接到服务器,然后使用命令去控制服务器。Windows 用户可以使用 Putty ,Mac 用户可以使用系统自带的终端工具或者一些第三方的工具eg: SSH Shell。然后用 ssh 命令,连接到你的服务器。像这样:

    ssh:root@你的服务器地址
    

    仓库

    为了可以使用 CentOS 系统的包管理工具去安装更多的东西,我们需要单独安装一些额外的软件仓库。

    EPEL

    sudo yum install epel-release -y
    

    IUS

    sudo yum install https://centos7.iuscommunity.org/ius-release.rpm -y
    

    IUS 仓库里面有我们需要的一些新的软件包,比如 PHP 7。注意我们安装的是为 CentOS 7 这个系统准备的 IUS 仓库,如果你的系统是 CentOS 6,你需要到 IUS 网站上去查找适合的仓库的下载地址。

    安装NGINX

    NGINX 这个软件包已经包装在 EPEL 仓库里了,之前我们已经安装了这个仓库,所以可以直接使用包管理工具去安装 NGINX。

    sudo yum install nginx -y
    

    启动

    sudo systemctl start nginx
    

    开启自启动

    sudo systemctl enable nginx
    

    服务启动以后,你就可以在浏览器上使用服务器的 IP 地址,或者指向这个地址的域名访问服务器指定的目录了。你会看到类似下面的图片,就说明可以了


    配置成功的页面

    配置 nginx 虚拟主机

    • 配置nginx的配置文件
    • 重新启动nginx {sudo systemctl reload nginx}

    配置文件如下图所示
    nginx的默认配置


    默认的相关配置

    nginx的配置


    nginx的配置

    安装uwsgi

    通过pip进行安装,如果安装权限不够,用sudo进行安装
    sudo pip install uwsgi
    

    uwsgi启动

    sudo  uwsgi --ini /etc/uwsgi.ini
    

    uwsgi 的相关配置

    uwsgi的配置

    这样就能访问相关网页了

    配置出现错误的信息

    *** Starting uWSGI 2.0.15 (64bit) on [Wed Dec 13 14:47:37 2017] ***
    compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 13 December 2017 14:46:26
    os: Linux-3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017
    nodename: moshimol
    machine: x86_64
    clock source: unix
    pcre jit disabled
    detected number of CPU cores: 1
    current working directory: /home/moshimol
    writing pidfile to /var/run/uwsgi.pid
    detected binary path: /usr/bin/uwsgi
    setgid() to 995
    setuid() to 997
    your processes number limit is 7283
    your memory page size is 4096 bytes
     ***    
    detected max file descriptor number: 65535
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    Listen queue size is greater than the system max net.core.somaxconn (128).
    

    说明listen 监听的过多,设置uwsgi的配置文件到相关100


    2017/12/13 15:00:00 [error] 6569#0: *307 connect() failed (111: Connection refused) while connecting to upstream, client: 115.236.171.18, server: 106.14.207.197, request: "GET /fitness/env HTTP/1.1", upstream: "uwsgi://127.0.0.1:6000", host: "106.14.207.197"
    2017/12/13 15:00:02 [error] 6569#0: *307 connect() failed (111: Connection refused) while connecting to upstream, client: 115.236.171.18, server: 106.14.207.197, request: "GET /fitness/env HTTP/1.1", upstream: "uwsgi://127.0.0.1:6000", host: "106.14.207.197"
    2017/12/13 15:00:02 [error] 6569#0: *307 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 115.236.171.18, server: 106.14.207.197, request: "GET /favicon.ico HTTP/1.1", host: "106.14.207.197", referrer: "http://106.14.207.197/fitness/env"
    
    

    一个API就写成功了

    http://106.14.207.197/fitness/env
    

    下一节进行阿里云OTS数据的增删改查

    相关文章

      网友评论

          本文标题:python+nginx+uwsgi的服务器搭建

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