Nginx:是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势
uwsgi::是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
运行过程:
nginx作为服务器的最前端,它将接受WEB的所有请求,统一管理请求。nginx把所有静态请求自己来处理(这是nginx的强项,静态文件像我们django博客项目中的static文件夹下面的图片,css,js)。然后nginx将所有的非静态请求(像显示文章的详细信息,通常这类信息都保存在数据库,因此需要调用数据库获取数据)通过uwsgi传递给Django,由django来处理,从而完成一次WEB请求。uwsgi的作用就类似一个桥接器,起到桥梁的作用。
阿里云服务器ubuntu14.04 (域名买了还没备案,所以博客暂时是通过ip访问的)
python3.5.2
nginx 1.4.6
uwsgi 2.0.15
django 1.11.3
markdown 1.0.1
打开putty远程控制云服务器输入账号和密码:
sudo apt-get install update 更新ubuntu库资源
sudo apt-get install nginx 安装nginx:
service nginx restart 启动nginx(重启)这个命令在后面要常用到
访问:http://59.110.155.51(这是我的云服务器的ip)
出现上图,则说明nginx启动成功
sudo apt-get install python3-pip
pip3 install uwsgi
测试uwsgi,创建test.py文件(注意:linux创建目录的命令是:mkdir,创建test.py用 “vi test.py”命令)
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
函数名字application,这是默认的函数,uwsgi的python加载器将会搜索这个名字
把它部署到htto端口8000(现在运行uwsgi来启动一个会把请求传递给你的wsgi应用的http服务器):
uwsgi --http :8000 --wsgi-file test.py
sudo apt-get install git 安装git
git clone https://github.com/xuna123/Django_study1.git
pip3 install django
pip3 install markdown
我们实现的目录:
在我们从git拉下来Django_study1项目的时候,在子目录下回自动帮我们生成wsgi.py文件,所以我们只需要创建
myweb_uwsgi.ini配置文件。
vim myweb_uwsgi.ini
文件内容:
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /root/Django_study1
# Django s wsgi file
module = easyblog.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
文件意思:
配置nginx:
接下来要做的就是修改nginx的配置文件,打开/etc/nginx/sites-available/default文件,删除所有内容,写入下面内容:
server {
listen 8099;
server_name 59.110.155.51
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 59.110.155.51:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /Django_study1/blog/static;
}
}
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['localhost','59.110.155.51']
现在我们重启nginx 和uwsgi:
service nginx restart
uwsgi --ini myweb_uwsgi.ini
参考资料:
http://zmrenwu.com/post/20/#购买域名
nginx restart failed问题解决方法:http://www.linuxidc.com/Linux/2015-07/119754.htm
虫师博客:http://www.cnblogs.com/fnng/p/5268633.html
http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html