linux部署python web_在linux上部署web环境

本文详细介绍了如何在Linux系统上部署Python Web环境,包括升级Python到2.7、安装pip、虚拟环境、nginx、gunicorn、supervisor以及MySQL。过程中遇到的问题如zlib、TLS/SSL缺失等,都有相应的解决方法。同时,还提到了vim配置和数据库重建的步骤。
摘要由CSDN通过智能技术生成

1.升级python到2.7版本(通过源码包重新安装一个2.7版本的python):

wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz

tar -zxf Python-2.7.9.tgz

./configure --prefix=/usr/local/python27/

make && make install

which python(查看可执行文件的位置)

ln -s python2.7 python(建立软链接)

2.让yum使用旧版2.6版本的python

vim /usr/bin/yum

把首行注释修改为 #!/usr/bin/python2.6

3.安装pip

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate

python get-pip.py

提示错误:zipimport.ZipImportError: can't decompress data; zlib not available

此时需要:

a-安装依赖zlib、zlib-devel

b-重新编译python:

./configure

在这里把Modules/Setup文件中的454行左右的,

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

去掉注释

zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

make && make install

重新做软链接,让系统路径的python指向新编译的python2.7版本

python get-pip.py

又失败了,提示:pip is configured with locations that require TLS/SSL

需要安装2个依赖:yum -y install openssl openssl-devel

再编译安装一次。。。。。

python get-pip.py(终于成功!)

pip被安装在python的可执行文件的同一目录。

做软链接 ln -s /usr/local/python27/bin/pip /usr/bin/pip

4.安装虚拟环境virtualenv和flask

pip install virtualenv(安装)

virtualenv --no-site-packages venv(建立虚拟环境,不安装任何系统中的第三方包)

5.安装nginx

yum install nginx(提示没有可用的源)

解决方法:

安装EPEL源(yum install epel-release)

又报错:Cannot retrieve metalink for repository: epel. Please verify its path and try again

解决方法:

编辑/etc/yum.repos.d/epel.repo 取消baseurl的注释,把mirrorlist注释掉。

设置nginx开机启动:

chkconfig nginx on

开启80端口,并重启iptables:

vim /etc/sysconfig/iptables   然后一定在第一个-A的行的上面一行添加一行,不然会失败:-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

service iptables restart

启动nginx:

service nginx start

6.安装gunicorn

pip install gunicorn(安装)

gunicorn -w 4 -b 127.0.0.1:端口号 文件名:app(直接运行gunicorn)

vim /etc/nginx/conf.d/default.conf(配置nginx):

server {

listen 80;

location / {

proxy_pass http://127.0.0.1:888; # 这里是指向 gunicorn host 的服务地址

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

重启nginx,ok!可以直接从80端口访问网站。

7.使用supervisor

最后一步,用进程管理工具supervisor把gunicorn作为服务来管理:

pip install supervisor

echo_supervisord_conf > supervisor.conf   # 生成 supervisor 默认配置文件

vim supervisor.conf                       # 修改 supervisor 配置文件,添加 gunicorn 进程管理

添加:

[program:入口文件名]

command=/root/aff/venv/bin/gunicorn -w4 -b0.0.0.0:888 入口文件名:app

directory=/root/aff

supervisord -c supervisor.conf(启动supervisor)

supervisorctl shutdown (关闭supervisor)

再启动时出现错误:Shut this program down first before starting supervisord.

解决办法:

unlink /tmp/supervisor.sock

再启动。

supervisor的基本使用命令

supervisord -c supervisor.conf                             通过配置文件启动supervisor

supervisorctl -c supervisor.conf status                    察看supervisor的状态

supervisorctl -c supervisor.conf reload                    重新载入 配置文件

supervisorctl -c supervisor.conf start [all]|[appname]     启动指定/所有 supervisor管理的程序进程

supervisorctl -c supervisor.conf stop [all]|[appname]      关闭指定/所有 supervisor管理的程序进程

8.安装mysql:

yum install -y mysql-server mysql mysql-devel (安装)

service mysqld start (启动)

chkconfig mysqld on (开机自启动)

mysqladmin -u root password 'new-password' (给root设置密码)

------------------------------------------

9.vim配置

vim配置(/etc/vimrc):

set nu(显示行号)

set expandtab(把tab转成空格)

set ts=4(一个tab等于4个空格)

set softtabstop=4(按退格时删除4个空格)

set autoindent(自动缩进)

解决vim没有颜色的方法:

原因是securecrt使用的终端是VT100,而vim的配置中只有终端是xterm才有颜色。

解决方法(修改配置文件):

vim ~/.bashrc

TERM=xterm

export TERM

然后用source命令重载一下配置文件。

10.重建数据库

直接使用flask的数据库模型重建:

python admin.py shell

from admin import db

db.create_all()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值