centos 7 安装 python django uwsgi ngnx

张映 发表于 2019-05-21

分类目录: 服务器相关

标签:, , , ,

python的环境以前搭过,到博客找了一下没有。做过的东西要记录很重要的。不然就是浪费时间。

对于gunicorn和uwsgi的选择,主要看个人喜好。gunicorn配置简单点。但是如果用nginx的话,用uwsgi好一点,nginx兼容了uwsgi。

1,安装依赖

 # yum install nginx python2-pip python-devel gcc openssl-devel libyaml-devel libffi-devel \
 readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf mysql-devel

错误1,

Running setup.py install for uwsgi ... error
Complete output from command /usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-N7CH0N/uwsgi/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-XWtx7A-record/install-record.txt --single-version-externally-managed --compile:
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'descriptions'
warnings.warn(msg)
running install
using profile: buildconf/default.ini
detected include path: ['/usr/include', '/usr/local/include']
Traceback (most recent call last):

缺少编译依赖

错误2:

ERROR: Complete output from command python setup.py egg_info:
ERROR: sh: mysql_config: 未找到命令
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-d35kJu/mysql-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

缺少mysql开发依赖

2,安装uwsgi,django

 # pip install uwsgi mysql-connector-python protobuf psutil pytz qrcode virtualenv \
websocket-client wheel web.py mysql-python django-cors-headers greenlet image "django<2"

根据需要,安装扩展。主要依据是代码用到哪些扩展。

3,测试uwsgi

# cat test.py
def application(env, start_response):
 start_response('200 OK', [('Content-Type','text/html')])
 return ["Hello World"]

# uwsgi --http :8000 --wsgi-file test.py

通过8000端口访问一下,出现Hello World,说明成功了

4,创建django项目根目录,创建启动脚本

# cat test.ini

[uwsgi]
http = :19090 //直接访问
socket = 127.0.0.1:19091 //反代用
wsgi-file = /var/www/mysite/wsgi.py
master = true
processes = 4
threads = 2
stats = 127.0.0.1:9191 //监控用

5,安装uwsgitop

# pip install --upgrade pip //升级pip
# pip install setuptools -U  //升级setuptoos
# pip install "uwsgitop==0.11"

要安装uwsgitop,setuptools的版本>=20.2.2

Collecting uwsgitop
Using cached https://files.pythonhosted.org/packages/8a/7a/9501084f6d2739b8a7d35ef5fb9cb539edb6e0a0d559366598b82bb7a96e/uwsgitop-0.11.tar.gz
Complete output from command python setup.py egg_info:
error in uwsgitop setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-QbKKdJ/uwsgitop/
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

碰到这样的问题就该升级了。

# uwsgitop 127.0.0.1:9191

uwsgi-2.0.18 - Mon May 20 10:15:31 2019 - req: 0 - RPS: 0 - lq: 0 - tx: 0
node: 127.0.0.1 - cwd: /var/www/mysite - uid: 0 - gid: 0 - masterpid: 9247
 WID    %       PID     REQ     RPS     EXC     SIG     STATUS  AVG     RSS     VSZ     TX      ReSpwn  HC      RunT    LastSpwn
 1	0.0     9252    0	0	0	0	idle    0ms     0	0	0	1	0	0	10:15:07
 2	0.0     9254    0	0	0	0	idle    0ms     0	0	0	1	0	0	10:15:07
 3	0.0     9256    0	0	0	0	idle    0ms     0	0	0	1	0	0	10:15:07
 4	0.0     9258    0	0	0	0	idle    0ms     0	0	0	1	0	0	10:15:07

6,nginx 反代

server {

    listen       80;
    server_name  tank.51yip.com;

    location / {
    	try_files $uri @test;
    }

    location @test {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:19091;
    }

    access_log /var/log/nginx/test.access.log;
    error_log /var/log/nginx/test.error.log;

}

systemctl start nginx &&  systemctl enable nginx,启动nginx和开机启动nginx

7,systemctl自启动uwsgi

# cat /usr/lib/systemd/system/test.service 

[Unit]
Description=test
After=syslog.target

[Service]
ExecStart=/usr/bin/uwsgi --ini /var/www/mysite/test.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

systemctl start test &&  systemctl enable test,启动uwsgi和开机启动uwsgi

# netstat -tpnl |grep uwsgi
tcp        0      0 0.0.0.0:19090           0.0.0.0:*               LISTEN      10520/uwsgi
tcp        0      0 127.0.0.1:19091         0.0.0.0:*               LISTEN      10520/uwsgi
tcp        0      0 127.0.0.1:9191          0.0.0.0:*               LISTEN      10520/uwsgi


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/2141.html