服务器是centos7的,首先从安装python3开始
安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc* make -y
然后安装python3
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
tar -Jxvf Python-3.5.2.tar.xz
cd Python-3.5.2
./configure prefix=/usr/local/python3
make && make install
之后建立软连接
# 需要先把python2的软连接python改名才能执行以下命令,不然会报错
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
python -V测试安装是否成功,pip - V测试pip是否安装成功
之后安装django
pip install django==2.0
之后安装uwsgi
pip install uwsgi==2.0.18
建立软连接,后期会很好用
ln -s /usr/local/python3/bin/django-admin.py /usr/bin/django-admin.py
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
现在可以直接输入uwsgi命令查看版本
uwsgi --version
# 2.0.18
现在可以在在任意目录创建一个测试demo,test.py内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
uWSGI Python 加载器将会搜索的默认函数 application 。
接下来我们启动 uWSGI 来运行一个 HTTP 服务器,将程序部署在HTTP端口 9090 上:
uwsgi --http :9090 --wsgi-file test.py
在浏览器中输入,
http://127.0.0.1:9090/
服务器没有浏览器展示的话新建一个终端输入
curl http:/127.0.0.1:9090/
查看是否会返回Hello word,返回则uwsgi安装成功,成功之后则说明以下组件堆栈有效
the web client <-> uWSGI <-> Python
# 结束uwsgi
pkill -9 uwsgi
接下来安装nginx
安装依赖包
yum -y install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
下载nginx压缩包安装
wget http://nginx.org/download/nginx-1.9.9.tar.gz
tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure
make
make install
切换到/usr/local/nginx/sbin目录下输入
./nginx
# 可以建立软连接就直接输入nginx命令
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
# 查看有没有nginx进程
ps -ef | grep nginx
nginx -s stop #停止nginx
nginx -s reload #重启nginx
然后可以在浏览器输入服务器地址出现welcome to nginx
即可成功,没有浏览器终端输入如下查看返回
curl 172.xx.xx.xxx
返回welcome to nginx即可成功
测试项目
现在将django项目复制到项目目录下,进入到项目目录(manage.py同级)
首先本地启动不会出错(python manage.py runserver 0.0.0.0:8090,能够请求的通就好,之后关掉服务),然后输入命令:
uwsgi --http :8090 --module mysite.wsgi
# 也可以这样启动,不用再项目目录
uwsgi --http :8090 --chdir /home/projects/mysite --wsgi-file mysite/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192
mysite是自己的项目,然后浏览器输入以下查看返回
http://172.xx.xx.xxx:8090/test/
返回正确则说明以下堆栈可正确运行
the web client <-> uWSGI <-> Django
现在就是最不省心的配置文件问题,在manage.py同级目录下新建一个uwsgi.ini文件,打开输入以下内容:
[uwsgi]
# Django-related settings
# 项目根目录的路径
chdir = /home/projects/mysite
# Django's wsgi file
#wsgi-file = hello/wsgi.py
module = mysite.wsgi:application
# uwsgi服务器的角色
master = true
# 进程7数
processes=4
# 线程数
threads=2
# 存放进程编号的文件
pidfile= uwsgi.pid
# the socket 要与nginx保持一致
socket = 127.0.0.1:8090
#http = 127.0.0.1:8013
stats = 127.0.0.1:9191
# ... with appropriate permissions - may be needed
#chmod-socket = 664
# clear environment on exit
# vacuum = true
之后启动这个配置文件就好,执行
uwsgi --ini uwsgi.ini
nginx配置文件,找到配置文件nginx.conf,我的在/usr/local/nginx/conf/nginx.conf,
以防万一,把原本的复制一份
cp nginx.conf nginx.conf.bak
然后打开修改文件nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php ap
plication/json text/json image/jpeg image/gif image/png application/octet-stream;
upstream django {
server 127.0.0.1:8090;
}
server {
listen 8000;
server_name 172.xx.xxx.xx;
charset utf-8;
client_max_body_size 75M;
access_log /var/log/host.access.log;
location / {
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_pass django;
}
# Django media
location /media {
alias /home/projects/mysite/media; #
}
location /static {
alias /home/projects/mysite/static; #
}
}
然后执行如下检查nginx语法问题
nginx -t
# 这是默认的nginx.conf文件地址(我的在/usr/local/nginx/conf/),如果nginx.conf放在其他位置可以执行
nginx -t /文件路径
出现以下结果说明正确
[root@k-node-app24 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
之后执行命令
nginx
# nginx.conf配置文件在其他位置时执行
nginx -c /文件路径
查看nginx是否启动
[root@k-node-app24 conf]# ps -ef | grep nginx
root 1134 128975 0 10:42 pts/0 00:00:00 grep --color=auto nginx
root 129438 1 0 10:02 ? 00:00:00 nginx: master process nginx
nobody 129439 129438 0 10:02 ? 00:00:00 nginx: worker process
uwsgi和nginx都启动成功后就可以浏览器输入地址或域名访问,这里小小的说明下浏览器访问的是nginx监听的接口,即listen 8000;例如:http://172.xx.xxx.xx:8000/hello/,就可以啦,本人拿8090端口访问了半天[囧][汗],一个啥都没访问出来哈哈哈,他们是这样来协作的,nginx监听8000端口的请求,把请求通过8090端口传给uwsgi,uwsgi监听的也是8090端口,然后uwsgi传给django服务,如下
the web client <-> the web server <-> the socket <-> uWSGI <-> Django
至此,nginx+uwsgi+django就部署好了,还有另外一种特别骚的部署方式,即用nohup启动,如下:
# 在manage.py同级目录输入
nohup python manage.py runserver 0.0.0.0:8090 >djo.out 2>&1 &
#返回如下示例则成功启动
[1]21615
# 查看启动日志
tail -f djo.out
# 关掉则输入如下命令
ps -aux | grep python|xargs kill -9
这样可以启动并且访问了,特别方便,不用配置uwsgi和nginx,骚吧哈哈哈,不好处就是一秒钟的请求量不能过大,一秒几百次请求一般是可以的,超过之后就危险,两种方式看情况选择哈哈
下面追加一下Flask+uwsgi+nginx部署的方式
其中安装uwsgi,费了好长时间
报错:
网上查找了半天说是依赖没有安装,那我们安装
yum install python3-devel
yum install gcc
完了之后还是红的,你就说气不气吧。。。 找了很久之后发现conda可以安装,我也安装成功了
conda install -c conda-forge libiconv
conda install uwsgi
可能是因为anaconda的原因吧,安装不成功
安装nginx的时候也有遇到问题。因为我们是一个空的Linux系统安装,所以会有依赖没有安装的问题
具体就是下面三个
make报错:安装pcre包,https://blog.51cto.com/favccxx/1620160
安装openssl包 https://blog.youkuaiyun.com/u014397092/article/details/78834869
报错解决办法: https://www.cnblogs.com/codeprojects/p/14051957.html
好了安装完成后,可以测试下uwsgi和nginx,用上面的测试方法就行。
后面就是配置文件的问题了,依旧是那么的烦人
uwsgi配置文件
[uwsgi]
master=true
socket = 127.0.0.1:8000
module = run:app
chdir = /home/yunshuo/PaddleOCR-release-2.3/
wsgi-file = /home/yunshuo/PaddleOCR-release-2.3/run.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:9191
pidfile=uwsgi.pid
touch-reload=/home/yunshuo/PaddleOCR-release-2.3
daemonize = /home/logs/flask/server.log
nginx配置文件
keepalive_timeout 65;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
server {
listen 8080;
server_name 121.x.x.x;
charset utf-8;
access_log /home/logs/host.access.log;
error_log /home/logs/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_param UWSGI_CHDIR /home/yunshuo/PaddleOCR-release-2.3;
uwsgi_param UWSGI_SCRIPT run:app;
}
location /static {
alias /home/yunshuo/PaddleOCR-release-2.3/static; #
}
web请求8080端口可以跑通了
参考文件uwsgi官方文档