记第一次博客

  python

及第一次博客的搭建

在用nginx与uwsgi转换之前使用Django 中的collectstatic 将所有的静态文件都集中在一个新的文件夹中

settings.py做如下设置

STATIC_ROOT = os.path.join(BASE_DIR,'static')

STATICFILES_FINDERS =(
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

配置静态文件,运行

python3 manage.py collectstatic

uwsgi

运行命令

pip3 install uwsgi

安装uwsgi后,对于django,运行命令

uwsgi --http :9000 --module Myblog.wsgi

进入 127.0.0.1:9000能够看到基本的窗口,这里有一个坑,我当时看到报了500的错,看到报错信息是 no module named  'math' ????

上网查看才发现需要重新编译uwsgi,于是下载了uwsgi的源码包,在根目录下运行

python3 uwsgiconfig.py --build

编译,然后使用该目录下的uwsgi运行端口,成功运行!

运行命令

sudo apt-get install nginx

安装nginx,将/etc/nginx/目录下的uwsgi_params拷贝到你的django的根目录下,与manage.py同级。

创建uwsgi.ini内容如下

[uwsgi]
http = :9000
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /root/Myblog
# Django's wsgi file
wsgi-file = Myblog/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
 
#monitor uwsgi status
stats = 127.0.0.1:9191


#store the log
pidfile = /root/log/master.pid
daemonize = /root/log/asscess.log

# clear environment on exit
vacuum          = true

运行命令

uwsgi --ini uwsgi.ini

nginx

在/etc/nginx/sites-enabled/ 中,需要将原来

default删除,建立新的nginx.conf,配置如下:

 
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
 
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media 
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static/ {
        alias /path/to/your/mysite/static/; # your Django project's static files - amend as required
    }
 
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

修改/static中的内容,将它指向你Django中用collectstatic后收集的静态文件,locations则为django目录下的uwsgi_params的绝对路径。

如果要使用https,测需要将nginx.conf配置为

# 将http的域名强制转换到https的流量中
server{
    listen 80;
    server_name christa.top;
    rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    # the port your site will be served on
    listen      443;
    # the domain name it will serve for
    server_name christa.top; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # SSL certifications setting
    ssl on;
    ssl_certificate /etc/nginx/cert/1801943_www.christa.top.pem;
    ssl_certificate_key /etc/nginx/cert/1801943_www.christa.top.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES1238-GCM-SHA256:ECDHE:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1  TLSv1.1  TLSv1.2;
    ssl_prefer_server_ciphers on;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static/ {
        alias /root/c1blog/static/; # your Django project's static files - amend as required
    }

    location /robots.txt{
       alias /root/c1blog/static/robots.txt;  # robots for search engine
   }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /root/c1blog/uwsgi_params; # the uwsgi_params file you installed
    }
}

注: location /static/  这个跳转一定要写成/static/如果写成/static则外部访问你的域名加上/static/后置时会访问到你的静态文件目录

运行命令

/etc/init.d/nginx restart

如果页面的静态文件还是无法显示,并且访问static目录返回403 Forbiden,那么很有可能是因为权限不够,解决方法为:

在/etc/nginx/nginx.conf中进行如下修改,给予更高的权限:

#user www-data;
user root;

访问你的服务器ip既可以看到一套完整的页面,然后照常配置WAF。至此,第一次博客搭建完成。今后就要开始多多想大佬学习啦,但是为什么Django响应速度这么慢呀sad