阿里云服务器配置(Ubuntu+Ngi

发布时间:2019-10-11 09:01:37编辑:auto阅读(1913)

    阿里云服务器配置(Ubuntu+Nginx+Flask)

    Ubuntu 16.04
    Nginx 1.12.0
    MongoDB 3.4
    Python 3

    环境配置

    配置 FTP 服务

    sudo apt-get install vsftpd

    启动 vsftpd 服务:

    sudo service vsftpd restart

    Windows 安装 FileZilla,输入主机、用户名、密码、端口,然后连接。

    Nginx 安装

    更改 nginx 安装源,以保证安装的是最新稳定版。:

    vim /etc/apt/sources.list

    添加:

    deb http://nginx.org/packages/ubuntu/ xenial nginx
    deb-src http://nginx.org/packages/ubuntu/ xenial nginx

    更新源,否则会报错

    sudo apt-get update

    安装 Nginx:

    sudo apt-get install nginx

    启动 Nginx 测试:

    sudo /etc/init.d/nginx start
    # 或者
    service nginx start

    此时打开浏览器访问你的服务器,就能看到经典的 Nginx 欢迎页面!
    参看:Nginx Install

    Python 相关

    安装 Python3 环境的 pip

    sudo apt-get install python3-pip

    安装创建独立的Python 环境所需的 virtualenv

    pip install virtualenv

    在指定路径下创建 Python3 虚拟环境:

    virtualenv -p /usr/bin/python3 py3env

    启动虚拟环境:

    source py3env/bin/activate

    退出虚拟环境:

    deactivate

    uWSGI

    配置复杂,用 Gunicorn 替代。

    进入虚拟Python 环境:

    pip3 install uwsgi

    Gunicorn

    使用 Gunicorn 配置更简单。在虚拟环境下,pip install gunicorn,安装 Gunicorn,新建配置文件 deploy_config.py,内容如下:

    import os
    bind='127.0.0.1:8080' #绑定的端口
    workers=4 #worker数量
    backlog=2048
    debug=True
    proc_name='gunicorn.pid'
    pidfile='/var/log/gunicorn/debug.log'
    loglevel='debug'

    启动 Gunicorn:

    gunicorn -c deploy_config.py myapp:app

    myapp 是入口Python文件名,app 是Flask 实例名。如果输出 worker 相关信息,表明启动成功。

    配置 Nginx

    修改 /etc/nginx/sites-available/ 下的defalut 文件为如下内容:

    server {
        listen 80;
        server_name example.com; # 这是HOST机器的外部域名,用IP地址也行
    
        location / {
            proxy_pass http://127.0.0.1:8080; # 这里是指向 gunicorn host 的服务地址
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }

    配置完了之后软链接一份到 /etc/nginx/sites-enabled/defalut 下面

    ln -s /etc/nginx/sites-available/defalut /etc/nginx/sites-enabled/defalut

    注:也可以删除default 文件的,新建自己的配置文件,并建立软链接。

    配置 Supervisor

    安装:

    apt-get install python-setuptools
    easy_install supervisor
    echo_supervisord_conf > /etc/supervisord.conf

    配置文件中添加:

    [program:myapp]
    command=/home/www/myapp/py3env/bin/gunicorn -c /home/www/myapp/deploy_config.py myapp:app
    autorstart=true
    directory=/home/www/myapp
    autorestart=true
    startsecs=10
    startretries=20
    
    [program:nginx]
    command=/usr/sbin/nginx
    startsecs=0
    stopwaitsecs=0
    autostart=true
    autorestart=true
    stdout_logfile=/var/deploy/log/nginx.log
    stderr_logfile=/var/deploy/log/nginx.err

    如出现端口占用的错误,则:

    sudo unlink /tmp/supervisor.sock
    sudo unlink /var/run/supervisor.sock

    启动 Supervisord:

    supervisord -c /etc/supervisord.conf

    关闭 supervisor:

    supervisorctl shutdown

    重新载入配置

    supervisorctl reload

    补充

    Linux 命令

    命令 功能 实例
    cp 复制文件或目录 cp file1 file2

    监听端口:

    lsof -i tcp | grep LISTEN
    
    ******************************
    sshd       837 root    3u  IPv4   8888      0t0  TCP *:ssh (LISTEN)
    vsftpd    4463 root    3u  IPv4  19989      0t0  TCP *:ftp (LISTEN)

    Nginx 知识补充

    tree /etc/nginx/
    /etc/nginx/
    ├── conf.d
    ├── fastcgi_params
    ├── koi-utf
    ├── koi-win
    ├── mime.types
    ├── naxsi_core.rules
    ├── naxsi.rules
    ├── naxsi-ui.conf.1.4.1
    ├── nginx.conf
    ├── proxy_params
    ├── scgi_params
    ├── sites-available
    │   └── default
    ├── sites-enabled
    │   └── default -> /etc/nginx/sites-available/default
    ├── uwsgi_params
    └── win-utf
    • 文件夹 sites-enabled 中的文件为 sites-available 文件夹中文件的硬链接。

    • 配置文件从 sites-avalidable中加载,默认配置文件为其中的default` 文件。

    • nginx.conf 为主配置文件。

    • uwsgi_parems 是与 Python 相关的文件。

    • fastcgi_parms 是与 PHP 相关的文件。

    • nginx 的默认网站目录 /usr/share/nginx/html

    常用命令:

    nginx -s stop  快速关闭 nginx
    nginx -s quit  优雅的关闭 nginx
    nginx -s reload  重新加载配置
    nginx -s reopen  重新打开日志文件

    获取所有运行中的 nginx 进程列表:

    ps -ax | grep nginx

    若 nginx 主进程 pid1628,则可用kill命令发送 QUIT 信号,关闭此进程:

    kill -s QUIT 进程ID

关键字