初来乍到,菜鸟一枚 , 仅为记录。。。
注意关闭防火墙(https://blog.csdn.net/ytangdigl/article/details/79796961)
服务器:Google Cloud Platform 下 CentOS 6 系统
1.安装nginx服务器
开始安装之前,确保已经将内置的应用升级到最新的可用版本
yum -y update
yum install openssl-devel
使用yum安装nginx,
sudo su //临时获取root权限
yum -y install nginx //安装nginx
yum 安装 nginx,我们可以通过 系统服务命令 service 来启动或停止
service nginx start #启动 nginx 服务
service nginx stop #停止 nginx 服务
service nginx restart #重启 nginx 服务
启动后,输入服务器IP可以看到
详细安装过程可参考
nginx服务器详细安装过程(使用yum 和 源码包两种安装方式,并说明其区别)
2.安装python(3.5.4)
wget https://www.Python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
tar xJf Python-3.5.4.tar.xz
cd Python-3.5.4
./configure //此处报了错误一
make //构建
make altinstall //安装
错误一:
configure: error: in `/home/linyi/Python-3.5.4':
configure: error: no acceptable C compiler found in $PATH
解决错误一:
//安装安装GCC软件套件
yum install gcc
python默认被安装在/usr/local/bin下,
而系统的默认python放置在/usr/bin下处(这个2.6是系统默认版本)
因此此时需要建立软连接指向当前系统默认的python目录处,让系统使用新的python版本
cd /usr/bin
mv python python2.0
cd /usr/local/bin
ln -s /usr/local/bin/python3.5 /usr/bin/python
1、修改了新的python版本,那么原先安装的python插件都不在新版本python目录下,因此需要重新安装
2、有些人更新了python版本无法使用yum,需要为yum修改它的配置文件
vi /usr/bin/yum
将文件头部的#!/usr/bin/python改成#!/usr/bin/python2.6(注意这个是之前的老版本python号,因为此时老版本才能支持yum)
3.安装flask
python pip3 install flask
ln -s /usr/local/bin/pip3 /usr/bin/pip
4.安装virtualenv
pip install virtualenv
创建应用目录demo
mkdir ******
在demo目录下创建一个python虚拟环境
/usr/local/bin/virtualenv -p /usr/bin/python venv
ls
可以使用-p PYTHON_EXE选项在创建虚拟环境的时候指定python版本(只能指定已安装好的python版本)
激活虚拟环境
[root@localhost demo]# source venv/bin/activate
激活后就可以在该环境下安装Python各种模块了
关闭虚拟环境的密令如下:
(venv)[root@localhost demo]# deactivate
在demo下安装flask,gunicorn
pip install flask
pip install gunicorn
demo下新建test.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "hello world"
@app.route('/hello')
def hello111():
return hello()
if __name__=='__main__':
app.run()
安装成功后,使用gunicorn运行基于flask的test.py文件
(venv)[root@localhost demo]# gunicorn --workers=3 test:app -b 127.0.0.1:8080
其中的–workes指定运行的进程数,后面的表示地址绑定
test:app中 test代表当前运行的module名,也就是文件名,后面的app是创建的Flask对象
具体配置的查看官网:http://docs.gunicorn.org/en/latest/settings.html#config
运行成功后,可以查看一下index进程
[root@localhost demo]# ps -ef | grep index
5.修改Nginx配置文件,具体如下:
#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 1024;
}
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;
server {
listen 80;
server_name pw.com;
location / {
#设置代理地址
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /static/ {
if (-f $request_filename) {
rewrite ^/static/(.*)$ /static/$1 break;
}
}
}
}
gunicorn启动python文件后,启动Nginx,直接访问本机的ip地址即可