2.服务器部署web服务器
#前几天按照文档走了一道,也记录了一下操作过程,其实挖了很多坑,没走通。又捯饬了几天终于在ubuntu成功运行了django+Nginx,记录如下
1.看图说话
- 首先使用django web开发框架(因为我不会html,css,js等,所以有个好用的开发框架将会开发简单,部署也简单)
- django使用python作为开发语言,开发完毕作为web应用程序运行在web服务器上,但是我们选择的web服务器例如Nginx,uWSGI都不认识python,所以web应用程序django要和web服务器进行交互则需要通信标准wsgi
- uWSGI是一种web服务器,它实现了WSGI协议、uwsgi、http等协议,我们选择uWSGI服务器运行django,他们之间使用wsgi协议通信。
- 同时为了提高网页的响应效率,有人采用网页的动静分析措施,比如利用Nguinx服务器处理/响应静态网页,uWSGI服务器响应动态网页。(我仅仅是听说)
- 为了达到动静分离的效果,我又安装了nginx,nginx与uWSGI两个web服务器协同工作则需要通信,他们的通信可以采用socket实现(这两个运行在服务器上的web服务器其实也就是两个运行程序嘛,他们之间相互调用/通信采用一种方法称为socket)
- 客户端,即用户的浏览器方位服务器端时,通过http协议与nginx通信,nginx处理静态请求/响应,然后动态请求/响应交给uWSGI。uWSGI调用python程序(django)处理请求,返回处理结果。
2.开始部署django
step1:
#安装django web开发框架
#安装conda的用户主要python和pip对应起来,文中的python和pip3皆是/usr/bin中的可执行程序
#python和pip对应错误可能出现调用包错误的现象
pip3 install django
#验证是否安装成功
django-admin.py startproject mysite #创建项目mysite
cd mysite #进入项目文件
/usr/bin/python3 runserver 0.0.0.0:8000 #8000端口运行django的测试文件,浏览器打开返回的地址会出现小火箭
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
#安装uWSGI web服务器
pip3 install uwsgi
#验证uwsgi是否安装成功
vim test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
uwsgi --http :8000 --wsgi-file test.py #8000端口运行test测试文件,浏览器打开返回的地址会出现hello world
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
#安装postgresql数据库的以及数据库操作接口
#安装数据库
sudo apt-get install postgresql postgresql-client #安装完毕后,系统会创建一个数据库超级用户 postgres,密码为空
pip3 install psycopg2-binary
#验证psycopg2是否安装成功
/usr/bin/python3
>>>import psycopg2
>>>dir(psycopg2)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
#安装nginx web服务器
#nginx负责静态web页面处理,uWSGI负责动态页面处理,实现动静分离,提高页面响应效率
sudo apt-get install nginx #安装成功nginx会自动启用
#检查nginx
sudo service nginx status
#或者访问浏览器出现欢迎界面
127.0.0.1:80
#激活或者stop、restart nginx
sudo sudo /etc/init.d/nginx start|stop|restart
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
#修改nginx的配置文件,具体请看《遇到的坑那一节》
#假设修改害了nginx的配置文件
cd mysite #进入mysite顶级项目文件夹
#
uwsgi --http :8000 --module mysite.wsgi #测试uwsgi与nginx是否可以联用,如果可以恭喜你,你实现了上面的图片内容
- 1
- 2
- 3
- 4
- 5
#推荐使用ini配置文件运行uwsgi
vim mysite_uwsgi.ini
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/mysite
# Django's wsgi file
module = mysite.wsgi:application
# the virtualenv (full path) (optional)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# the socket (use the full path to be safe
socket = 127.0.0.1:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# background the process
daemonize=/path/to/mysite/mysite.log #后台运行uwsgi服务器
pidfile=/path/to/mysite/mysite.pid #储存uwsgi运行时的pid,可以用于终止uwsgi服务器
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
#启动web服务器
uwsgi --ini /path/to/mysite_uwsgi.ini
#查看服务
ps -aux |grep "uwsgi"
#访问你自己的项目
127.0.0.1:8000 #注意我们设置nginx监听的端口是8000,而8001端口是nginx与uWSGI通信的端口
#停止
uwsgi --stop /path/to/mysite/mysite.pid
或者
sudo pkill -f uwsgi -9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.遇到的坑
- apache2与nginx重复安装,重复卸载,最后两个web服务器懂启动不了,好吧!最后卸载apache2,apt安装nginx
$ which nginx
/usr/sbin/nginx #我也不知道为啥到sbin里面去了
$ sudo /usr/sbin/nginx -t #测试nginx的配置文件等是否正确,而且会告诉你配置文件位置
$ sudo /usr/sbin/nginx #启动nginx
$ ps -aux |grep "nginx" #验证nginx启动成功
$ sudo /usr/sbin/nginx -s stop #显将nginx停了,我们要去修改配置文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- nginx配置文件的坑
$ sudo /usr/sbin/nginx -t 告诉我nginx配置文件在/etc/nginx/conf.d/*.conf ;有些人的配置文件在/etc/nginx/sites-enabled/文件夹里
- 1
#如果配置文件在/etc/nginx/sites-enbled,操作如下
#create /etc/nginx/sites-available/ directory and put this in it
#sites-available文件夹包括一些可配置文件
#sites-enabled文件夹包括一些已经配置了的文件
#最好不要修改nginx.conf文件
#在sites-available文件夹创建mysite_nginx.conf文件并写入如下内容
---------------------------------------------------------------------------------------------------------
# mysite_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) nginx与uWSGI通信使用8001端口
}
# configuration of the server
server {
# the port your site will be served on
listen 8000; #nginx监听8000端口
# the domain name it will serve for
server_name 172.XXX.XXX.XXX; # 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 #/etc/nginx/uwsgi_params;
}
}
----------------------------------------------------------------------------------------------------------------------------
#symlink to this file from /etc/nginx/site-enabled so nginx can see it
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
#重启nginx服务
sudo /etc/init.d/nginx restart 或者 sudo /usr/sbin/nginx
ps -aux |grep nginx
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
#如果配置文件在/etc/nginx/conf.d/,操作如下
-----------------------------------------------------------------------------------------------------
# mysite_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) nginx与uWSGI通信使用8001端口
}
# configuration of the server
server {
# the port your site will be served on
listen 8000; #nginx监听8000端口
# the domain name it will serve for
server_name 172.XXX.XXX.XXX; # 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 #/etc/nginx/uwsgi_params;
}
}
----------------------------------------------------------------------------------------------------------------------------
#symlink to this file from /etc/nginx/conf.d/ so nginx can see it
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/conf.d/
#重启nginx服务
sudo /etc/init.d/nginx restart 或者 sudo /usr/sbin/nginx
ps -aux |grep nginx
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- nginx欢迎页面一直存在
#方法一
sudo rm /etc/nginx/sites-enabled/default.conf
#方法二
修改default.conf中监听的端口,让默认监听的端口与刚才mysite-nginx.conf监听的端口不一样
- 1
- 2
- 3
- 4
- uwsgi安装失败
有没有安装python-dev包?
- 1
- uwsgi调用失败
pip 是不是使用的conda安装的?
- 1
4.声明
纯粹用于记录,交流学习也是可行的,没有任何教学目的,请根据自己的实际情况操作自己的系统。
推荐阅读