1,349

在CentOs 下配置Nginx、supervisor、Gunicorn

1、nginx 安装
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
重启nginx: /etc/init.d/nginx restart
查看是否正常: nginx -t
注意:配置文件会在/usr/local/nginx/conf下,
如果需要自定义配置需要在该目录下,
之前的Ubuntu是在etc根目录下,
一开始我也再etc底下配置,一直出错,后来才发是目录搞错了。
nginx 配置文件如下:
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default;

#error_page 404 /404.html;

# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

include enable-php.conf;

location /nginx_status
{
stub_status on;
access_log off;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

location ~ /.well-known {
allow all;
}

location ~ /\.
{
deny all;
}

access_log /home/wwwlogs/access.log;
}

#以下为自定义新增端口配置
server {
listen 8800 default_server;
listen [::]:8800 default_server ipv6only=on;
server_name 192.168.128.1;

location /static {
alias /home/www/finance/app/static;
}

location / {
proxy_pass http://127.0.0.1:7055;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 500s;
proxy_read_timeout 500s;
proxy_send_timeout 500s;
}
}
注意:每修改过一次配置文件都需要用重启nginx更新配置才得以生效
2、centos7安装supervisor详细教程
一开始用pip install supervisor 安装发现安装不上,报错如下:
Supervisor requires Python 2.4 or later but does not work on any version of Python 3. You are using version 3.6.6 (default, Dec 19 2018, 00:11:02)
[GCC 4.9.2 20150212 (Red Hat 4.9.2-6)]. Please install using a supported version.

—————————————-
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-install-ze2riqy0/supervisor/
#奇怪的是我的默认python版本就是2.X,却无法正常安装,所以我采用第二种方式安装:
# easy_install supervisor #可以正常安装
#echo_supervisord_conf>/etc/supervisord.conf #生成配置文件
#用easy_install安装supervisor,偶尔会遇到supervisor.sock找不到的错误,这样我们只能手动在该目录下创建一下
#sudo touch /var/run/supervisor.sock
#sudo chmod 777 /var/run/supervisor.sock
#sudo service supervisor restart
# yum install epel-release
# yum install -y supervisor
# systemctl enable supervisord # 开机自启动
# systemctl start supervisord # 启动supervisord服务
# systemctl status supervisord # 查看supervisord服务状态
# ps -ef|grep supervisord # 查看是否存在supervisord进程
注意:每修改过一次配置文件都需要用supervisorctl update更新配置才得以生效
3、gunicorn 安装
pip install gunicorn
# 直接运行的方法
python manage.py runserver
# 使用Gunicorn运行的方法
# 这里需要注意一下, 冒号前面的是文件名也就是manage.py而后面的是应用的名称。我在这里载过跟头。因为服务器需要接受一个wsgi的应用而manager = Manager(app)不是一个wsgi应用所以报错
gunicorn manage:app
gunicorn –workers=3 manage:app -b 0.0.0.0:8080
结合supervisor后的配置文件:
[program:test]
command = /home/www/test/venv/bin/gunicorn -b 127.0.0.1:7055 manage:app
directory = /home/www/test

综合以上三大神器就很方便的实现了项目部署发布。
附加笔记:
nginx配置web前端js,css, html压缩传到前台
gzip_min_length 2k; #设置最小压缩大小
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 4; #设置压缩率
gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable “msie6”;

测试nginx配置是否正常:
nginx -t

平滑重启nginx:
nginx -s reload

压缩:
tar zcvf 压缩后.tar.gz 压缩前文件
解压:
tar zxvf 压缩后.tar.gz

静态文件(jquey,bootstrip等等)最好引用cdn路径:
http://www.staticfile.org/

发表评论

邮箱地址不会被公开。 必填项已用*标注