Django web服务本身不被Nginx以及Apache服务器支持,Nginx和Apache的主要功能就是在服务器上运行http web服务。但完成的django网站需要使用Nginx或者Apache进行运行,所以采用uwsgi web服务器作为中间服务器,其作为中间的桥梁来连接django项目和服务器。
关于域名解析
域名解析就是将域名和ip绑定到一起,访问域名就是访问ip。http端口默认为80, https端口为443。
具体的执行步骤如下
项目搭建前的准备:关闭防火墙检查环境
为了顺利部署先关闭防火墙
从windows上查看linux主机是否能联通
在linux下安装python3
python3的安装步骤查看之前博客,此处不再啰嗦。
安装mysql
查找mariadb安装包
安装所有的mariadb包:yum install mariadb* -y
等到mysql安装好之后启动mysql,命令为:systemctl start mariadb。
检查数据库能否使用
创建数据库,命令:create database 数据库名称 charset=utf8;。
安装Django环境
使用xftp把本地项目上传到远程虚拟机,(导出windows上的package.txt包使用的命令为:pip freeze > package.txt)
进入虚拟机查看文件是否上传成功。
文件上传成功之后在linux上安装package.txt内的安装包。命令为pip3 install -r package.txt。
注:安装的时候可能遇到网慢,再重新执行安装命令即可。有可能还需要更新下载包按照下面的方法更新就是。
使用django命令启动项目,监听本地所有ip的80端口。
输入自己的ip地址,测试能否联通。
结果如下:
注:等到项目真正部署的时候需要把settings中的ALLOWED_HOST改为*,允许所有用户访问,DEBUG关闭。
安装uwsgi
使用命令:pip install uwsgi,如果慢的话使用国内源安装。
使用国内源下载,命令为 pip install uwsgi -i https://pypi.doubanio.com/simple。一般不建议这么做因为国内源和国外源的安装可能会有冲突。
配置软连接,下图配置软连接命令如下,在项目目录下执行:ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
注:下图是python3的安装位置。
测试uwsgi
命令为:uwsgi --http 10.10.21.178:80 --file FreshShop/wsgi.py --static-map=/static=static
10.10.21.178:80为本地ip和端口,在云主机上ip需要时内网ip,FreshShop为本地项目目录。
注意在项目目录下输入下面的命令,否则会找不到网页。
测试:这个时候走的是uwsgi
配置uwsgi,使用uwsgi脚本运行项目,在opt下创建script脚本目录,在脚本目录下写入uwsgi.ini文件。
uwsgi.ini脚本内容如下:
uwsgi.ini内容如下:
[uwsgi] chdir=/opt/FreshShop module=FreshShop.wsgi:application socket=/opt/script/uwsgi.sock workers=5 pidfile=/opt/script/uwsgi.pid http=10.10.21.178:80 static-map=/static=/opt/FreshShop/static uid=root gid=root master=true vacuum=true enable-threads=true thunder-lock=true harakiri=30 post-buffering=4096 daemonize=/opt/script/uwsgi.log
注释过的内容如下:
[uwsgi] chdir=/opt/FreshShop #项目目录 module=FreshShop.wsgi:application #指定项目的application socket=/opt/script/uwsgi.sock #指定sock的文件路径 workers=5 #进程个数 pidfile=/opt/script/uwsgi.pid http=10.10.21.178:80 #指定IP端口,上线部署时时内网ip static-map=/static=/opt/FreshShop/static #指定静态文件 uid=root #用户 gid=root #组 master=true #启用主进程 vacuum=true #自动移除unix Socket和pid文件当服务停止的时候 enable-threads=true #启用线程 thunder-lock=true #序列化接受的内容,如果可能的话 harakiri=30 #设置自中断时间 post-buffering=4096 #设置缓冲 daemonize=/opt/script/uwsgi.log #设置日志目录
调用启动uwsgi.ini脚本,命令为:uwsgi --ini uwsgi.ini。
启动成功会在当前目录下生成四个文件
注:如果是三个文件,到uwsgi.log下查找日志,查找错误原因。当前校验步骤适用于虚拟机,云主机访问不到。
注:如果生成三个文件的话查看是否是配置文件的sock没开,或者端口被占用。
如果端口被占用了使用命令:lsof -i :80查看端口被谁占用了,然后杀死进程重新执行命令。
测试:这个时候查看的是uwsgi
安装Nginx服务器
下载nginx,nginx在哪个目录下执行就下载到哪个目录下。这里在opt目录下执行,就下载到opt目录下。
命令:wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
解压:下载完后之后在当前目录下解压文件,并进入解压完成的文件。
命令:tar -zxvf nginx-1.12.2.tar.gz && cd nginx-1.12.2
进入解压完成的文件后,开始编译安装,命令:./configure \ 再按两次回车键。
命令:make&&make install
那么编译安装完成之后,nginx安装到哪里了呢?如下:
Nginx的命令安装到哪里了呢?在如下目录,所有软连接也要在sbin下创建。
在当前目录下创建软连接。
命令:ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
启动nginx进行测试,启动nginx命令:nginx。
注:如果端口被占用,查找被占用的端口。
注:一般情况下,是上面配置的uwsgi占用的,所以杀死uwsgi进程,再重新启动Nginx
测试:如下图所示,nginx启动成功
修改nginx.conf配置文件。
进入上一层找到conf
进入conf找到nginx.conf
为防止配置错误,需要备份配置文件。
进入修改nginx.conf文件
解开
Server_name可改可不改,添加一句gzip_types。
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
注:最后不要有空格
添加错误日志:error_log /var/log/nginx/error.log error;注意error_log这个文件可能没有,需要自己创建。
手动创建错误日志目录
Location下面的两句话删掉
删除之后加上下面三句话
include uwsgi_params; nginx默认不兼容uwsgi,所以需要加载uwsgi模块
uwsgi_connect_timeout 30; 设置连键超时时间
uwsgi_pass unix:/opt/script/uwsgi.sock; nginx对应的uwsgi socket文件,连接这里进行通信。
删除掉下面的内容
删除完成后再原位置加入下面的内容
配置文件修改完成,保存退出。
配置文件内容如下:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name FreshShop; charset utf-8; access_log logs/host.access.log main; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_log /var/log/nginx/error.log error; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/opt/script/uwsgi.sock; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # location = /static/ { alias /opt/FreshShop/static; index index.html index.htm; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
进入uwsgi.ini修改配置
改成8000
杀掉进程重新开启
在script目录下启动
这个时候通过nginx转uwsgi转django就成功了