nginx + gunicorn + django的简单部署

sudo apt-get install nginx

cd /etc/nginx/sites-available

sudo vim default

原本是想安装Image包,但是百度的方法都乱七八糟的不能用,最后是参考这篇文章的方法安装PIL的,Image是PIL的一个模块。 
方法非常简单,两个步骤:

  1. 安装相应的库和包:

    sudo apt-get build-dep python-imaging
       
       
    • 1
    • 1
  2. ubuntu14.04中libfreetype 的头文件在目录/usr/include/freetype2下,但PIL中使用的路径是freetype,所以将原来的目录链接到新建的freetype:

    cd /usr/include
    sudo ln -s freetype2 freetype
       
       
    • 1
    • 2
    • 1
    • 2

    最后忍不住想吐槽百度简直是程序员的噩梦,这么简单的问题都找不到好答案

>>> import django
>>> print django.get_version()

django-admin.py startproject myweb
python manage.py startapp learn



将配置文件修改为这样的 注意自己修改下面的路径

server {
     listen 80;

     server_name server_name;
     access_log /home/virusdefender/Desktop/access.log;
     error_log /home/virusdefender/Desktop/error.log;

     location / {
         proxy_pass http://127.0.0.1:8020;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
}
sudo nginx -t 测试配置文件

sudo nginx -s reload 重启nginx

然后去代码目录运行python manage.py runserver 8020测试一下

这个时候应该就能访问到了

然后运行sudo pip install gunicorn

去配置gunicorn代替runserver

在django的settings INSTALLED_APPS里面加上gunicorn

在manage.py文件夹中运行

gunicorn ×××.wsgi:application -b 127.0.0.1:8020 --reload

×××是工程名字 也就是settings所在的目录的名字 ip和端口要和上面的那个一样

这个时候应该就可以了

如果是ssh 连接 使用命令

nohup gunicorn ×××.wsgi:application -b 127.0.0.1:8011 --reload&

其实就是前面添加nohup 最后面加上&符号

进程操作的一点知识

停止进程操作 停止操作是通过向进程发送信号来进行的

步骤1:查询进程的主进程号,以nginx为例 ps -ef | grep nginx 在进程列表里面找master进程,它的编号就是主进程号了。

步骤2:发送信号 从容停止Nginx: kill -QUIT 主进程号

快速停止Nginx: kill -TERM 主进程号

强制停止Nginx: pkill -9 nginx

平滑重启 如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。

平滑重启命令: kill -HUP 主进程号或进程号文件路径 或者使用

sudo nginx -s reload


托管 Django Web 应用程序相当简单,虽然它比标准的 PHP 应用程序更复杂一些。 让 Web 服务器对接 Django 的方法有很多。 Gunicorn 就是其中最简单的一个。

Gunicorn(Green Unicorn 的缩写)在你的 Web 服务器 Django 之间作为中间服务器使用,在这里,Web 服务器就是 Nginx。 Gunicorn 服务于应用程序,而 Nginx 处理静态内容。

Gunicorn

安装

使用 Pip 安装 Gunicorn 是超级简单的。 如果你已经使用 virtualenv 搭建好了你的 Django 项目,那么你就有了 Pip,并且应该熟悉 Pip 的工作方式。 所以,在你的 virtualenv 中安装 Gunicorn。

  1. $ pip install gunicorn 

配置

Gunicorn 最有吸引力的一个地方就是它的配置非常简单。处理配置最好的方法就是在 Django 项目的根目录下创建一个名叫 Gunicorn 的文件夹。然后在该文件夹内,创建一个配置文件。

在本篇教程中,配置文件名称是 gunicorn-conf.py。在该文件中,创建类似于下面的配置:

import multiprocessing
bind = "127.0.0.1:8000"
workers = 2
errorlog = "/home/nginxuser/example/gunicorn.error.log"
#loglevel = "debug"
proc_name = "gunicorn_example"


  1. import multiprocessing 
  2. bind = "127.0.0.1:8000"
  3. workers = multiprocessing.cpu_count() * 2 + 1 
  4. reload = True 
  5. daemon = True 

在上述配置的情况下,Gunicorn 会在 /tmp/ 目录下创建一个名为 gunicorn1.sock 的 Unix 套接字。 还会启动一些工作进程,进程数量相当于 CPU 内核数量的 2 倍。 它还会自动重新加载并作为守护进程运行。

/home/wwwroot/myweb/gunicorn

运行

Gunicorn 的运行命令有点长,指定了一些附加的配置项。 最重要的部分是将 Gunicorn 指向你项目的 .wsgi 文件。

  1. gunicorn -c gunicorn/gunicorn-conf.py -D --error-logfile gunicorn/error.log myweb.wsgi 

上面的命令应该从项目的根目录运行。 -c 选项告诉 Gunicorn 使用你创建的配置文件。 -D 再次指定 gunicorn 为守护进程。 最后一部分指定 Gunicorn 的错误日志文件在你创建 Gunicorn 文件夹中的位置。 命令结束部分就是为 Gunicorn 指定 .wsgi 文件的位置。


yum install nginx

检查配置是否有错

nginx -t -c /etc/nginx/nginx.conf

启动nginx

service nginx start

设置开机自启

systemctl enable nginx

创建用户

useradd nginxuser
passwd nginxuser

修改nginx主配置

vim /etc/nginx/nginx.conf

非注释首行

user nginx

改为

user nginxuser

不然可能会出现网站静态文件访问报403问题。

新建网站运行配置

mkdir /etc/nginx/site-available/example.conf
server {                                                               
    listen      80;                                                    
    server_name example.com;                            
    charset     utf-8;                                                 
    client_max_body_size 75M;                                          
    access_log /home/nginxuser/projects/example/nginxlogs/access.log;
    error_log /home/nginxuser/projects/example/nginxlogs/error.log;          
 
    location /static {                                                 
        alias /home/nginxuser/projects/explame/static;                
    }                                                                  
 
    location / {                                                       
        proxy_pass http://127.0.0.1:8000;                              
        proxy_set_header Host $host;                                   
        proxy_set_header X-Real-IP $remote_addr;                       
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
    }                                                                  
}                                                                       jk

建立软链接

ln -s /etc/nginx/sites-available/explame.conf /etc/nginx/sites-enabled/explame.conf

Gunicorn

安装

pip install gunicorn

项目根目录下添加gunicorn运行配置文件gunicorn.conf.py

import multiprocessing
bind = "127.0.0.1:8000"
workers = 2
errorlog = "/home/nginxuser/example/gunicorn.error.log"
#loglevel = "debug"
proc_name = "gunicorn_example"

启动

sudo gunicorn example.nginx_wsgi:application -c /home/nginxuser/projects/example/gunicorn.conf.py

后台运行

sudo nohup gunicorn example.nginx_wsgi:application -c /home/nginxuser/projects/example/gunicorn.conf.py&

如果运行报错先使用以下命令检查下nginx配置是否有错

nginx -t -c /etc/nginx/nginx.conf
 
   
 
   
 
   

接上一章:已经按照之前章节安装了Python2.7.10,Django 1.8.14. 以及mysql并创建了数据库以及数据库帐号密码。

一、安装nginx

yum -y install nginx
#设置nginx开机启动:
chkconfig nginx on
启动nginx:service nginx start

  如果安装不了或者找不到nginx的源,则手动添加文件:

1
/etc/yum .repos.d /nginx .repo

  在里面填入:

1
2
3
4
5
6
7
8
9
[nginx]
 
name=nginx repo
 
baseurl=http: //nginx .org /packages/centos/6/ $basearch/
 
gpgcheck=0
 
enabled=1

  然后再执行一次yum -y install nginx  

启动报错:nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)

  解决办法:vim /etc/nginx/conf.d/default.conf

  将:   listen       80 default_server; 

            listen       [::]:80 default_server;

  改为: listen       80;
             #listen       [::]:80 default_server;

  启动nginx就行了。。

二、配置nginx

vi /etc/nginx/nginx.conf

     (修改成自己的用户账号,它默认用的是nginx用户,但我们博客的目录下访问这些是需要用户账号的,而nginx这个用户是没有权限的。所以将user后改成自己的用户账号brad。如果不做这些修改,我们后面访问静态文件会出现问题。因为nginx本来就没有权限去访问这些静态的文件。)

    

  


三、安装gunicorn

  1、安装
/usr/local/bin/pip install gunicorn
  2、 在xxx_project下新建gunicorn的配置文件gunicorn.conf.py
复制代码
import multiprocessing

bind = "127.0.0.1:8080"
workers = 2
errorlog = '/home/brad/xxx_project/gunicorn.error.log'
#accesslog = './gunicorn.access.log'
#loglevel = 'debug'
proc_name = 'gunicorn_blog_project'
复制代码
  3、在XXX_project下新建nginx的配置文件nginx.conf 
复制代码
server {
     listen 80;
     server_name localhost example.com;
     access_log /home/brad/xxx_project/nginx.access.log;
     error_log /home/brad/xxx_project/nginx.error.log;

     location / {
         proxy_pass http://127.0.0.1:8080;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }

     location /robots.txt {
        alias /home/brad/xxx_project/static/robots.txt;
     }

     location /favicon.ico {
          alias /home/brad/xxx_project/static/img/favicon.ico;
     }

     location ~ ^/(media|static)/  {
         root    /home/brad/xxx_project;
         expires 30d;
     }


     # this prevents hidden files (beginning with a period) from being served
      location ~ /\. { 
        access_log off; log_not_found off; deny all;
     }

}
复制代码
  4、将其链接到 /etc/nginx/conf.d/blog_project.conf (需要root帐号权限或者sudo)
sudo ln -s /home/brad/xxx_project/nginx.conf /etc/nginx/conf.d/xxx_project.conf
  5、同时,必须把nginx.conf里server_name后面的内容(localhost)加入到 settings.py里的ALLOWED_HOSTS
sudo vi settings.py

  

  6、现在我们现在可以运行一下gunicorn:
sudo nohup /usr/local/python2710/bin/gunicorn xz1024_project.wsgi:application -c /home/brad/xz1024_project/gunicorn.conf.py& 

  注意:需要在/home/brad/xxx_project目录下面执行。

  8.如果要外部访问,则打开80端口:
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

sudo service iptatbles save

 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值