Ubuntu+NGINX+uwsgi搭建web服务器

软件版本说明

  • Ubuntu 14.04.6 LTS
  • nginx/1.4.6
  • uwsgi/2.0.18
  • python3.6.3

基本流程

  • 利用flask搭建web服务
  • 安装并配置uwsgi
  • 安装并配置nginx
  • 安装并配置supervisord
  • 启动服务

开始搭建

利用flask搭建web服务,进入Ubuntu系统进行以下操作

如果未安装git,apt install git

  • 如果使用pycharm或者其他编辑器,打开项目之后请先配置好解释器路径,打开编辑器内置terminal终端,然后依次运行以下命令(或者**虚拟环境,source venv/bin/activate

pip install -r requirements.txt
python app.py runserver

  • 在浏览器打开‘http://127.0.0.0.1:5000’,看到’hello, world!’,最简单的flask服务已经搭建完成

安装uwsgi并配置(以下操作均默认已**虚拟环境)

  • pip install uwsgi
  • 在fd目录中创建uwsgi.ini文件,内容为

[uwsgi]
socket = 127.0.0.1:5000
wsgi-file = app.py
chdir = /path/to/fd #写fd项目路径

  • 在命令行输入uwsgi uwsgi.ini
  • 在浏览器打开‘http://127.0.0.1:5000’,看到’hello, world!'表示uwsgi配置成功

安装nginx并配置

  • apt install nginx
  • 编辑/etc/nginx/nginx.conf,没有的话vim /etc/nginx/nginx.conf创建,内容如下(复制即可)

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 102400;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 600;
#gzip on;
server {
listen 80;
server_name 127.0.0.1; #你主机的ip
charset utf-8;
#access_log logs/host.access.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000; #此处设置与uwsgi.ini一致
uwsgi_param UWSGI_PYHOME /path/to/venv; #此处需更改成自己的解释器
uwsgi_param UWSGI_CHDIR /path/to/fd; #此处是项目位置
uwsgi_param UWSGI_SCRIPT app:application; #文件名:应用名
# include uwsgi_params;
# root /var/flasky/app/templates;
# index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /.ht {
# deny all;
#}
}
}

  • service nginx start #启动nginx服务,这一步之前需确认启动了uwsgi
  • 在浏览器访问’http://ip:port’看到’hello, world!'表示成功

安装并配置supervisor

  • apt install supervisor
  • 配置/etc/supervisor/conf.d/my_supervisor.conf,没有的话创建,vim /etc/supervisor/conf.d/my_supervisor.conf,添加如下内容

[program:fd] #冒号后面为项目目录名称
command=/path/to/uwsgi /path/to/uwsgi.ini
directory=/path/to/fd #项目路径
user=root
autostart=true
autorestart=true
stdout_logfile=/path/to/uwsgi_supervisor.log #先创建该日志文件

  • service supervisor start #启动supervisor,启动uwsgi服务,启动nginx,即可访问

访问逻辑

Ubuntu+NGINX+uwsgi搭建web服务器

  • 基本逻辑是,浏览器输入网址,请求发送到nginx服务器,如果是静态文件则直接返回给客户端,否则将请求转发给uwsgi,uwsgi调用flask的处理逻辑,获得返回,然后沿着原路径返回请求给客户端。

写在最后

由于作者水平有限,文章难免出现错误和理解不到位之处,请各位多多谅解,同时欢迎各位指出问题,提出建议,大家一起交流学习。

参考文章

1.访问逻辑图参考
2.关于NGINX
3.关于uwsgi_parma
4.关于uwsgi