文章目录

  • 1.看图说话
  • 2.开始部署django
  • 3.遇到的坑
  • 4.声明

#前几天按照文档走了一道,也记录了一下操作过程,其实挖了很多坑,没走通。又捯饬了几天终于在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的测试文件,浏览器打开返回的地址会出现小火箭
#安装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"] # python2uwsgi --http :8000 --wsgi-file test.py #8000端口运行test测试文件,浏览器打开返回的地址会出现hello world
#安装postgresql数据库的以及数据库操作接口
#安装数据库
sudo apt-get install postgresql postgresql-client #安装完毕后,系统会创建一个数据库超级用户 postgres,密码为空pip3 install psycopg2-binary
#验证psycopg2是否安装成功
/usr/bin/python3>>>import psycopg2
>>>dir(psycopg2)
#安装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
#修改nginx的配置文件,具体请看《遇到的坑那一节》
#假设修改害了nginx的配置文件
cd mysite #进入mysite顶级项目文件夹
#
uwsgi --http :8000 --module mysite.wsgi  #测试uwsgi与nginx是否可以联用,如果可以恭喜你,你实现了上面的图片内容
#推荐使用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服务器
#启动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

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停了,我们要去修改配置文件
  • nginx配置文件的坑
$ sudo /usr/sbin/nginx -t 告诉我nginx配置文件在/etc/nginx/conf.d/*.conf   ;有些人的配置文件在/etc/nginx/sites-enabled/文件夹里
#如果配置文件在/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 socketserver 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 onlisten      8000; #nginx监听8000端口# the domain name it will serve forserver_name 172.XXX.XXX.XXX; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /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
#如果配置文件在/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 socketserver 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 onlisten      8000; #nginx监听8000端口# the domain name it will serve forserver_name 172.XXX.XXX.XXX; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /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
  • nginx欢迎页面一直存在
#方法一
sudo rm /etc/nginx/sites-enabled/default.conf
#方法二
修改default.conf中监听的端口,让默认监听的端口与刚才mysite-nginx.conf监听的端口不一样
  • uwsgi安装失败
有没有安装python-dev包?
  • uwsgi调用失败
pip 是不是使用的conda安装的?

4.声明

纯粹用于记录,交流学习也是可行的,没有任何教学目的,请根据自己的实际情况操作自己的系统。

2.服务器部署web服务器相关推荐

  1. 怎样在linux上部署web服务器

    linux上部署web服务器 1.从Apache官网下载源码. 2.卸载原来已经存在的httpd. 3.挂载光盘准备部署httpd. 4.源码编译安装httpd, 解包 , 配置(--prefix:指 ...

  2. Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器

    Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器 一.搭建NginxWeb服务器     此种方式是用yum安装Nginx,为保证安装成功需在安装之前提前安装epel扩展源.     用 ...

  3. Nginx实战基础篇一 源码包编译安装部署web服务器

    Nginx实战基础篇一 源码包编译安装部署web服务器 版权声明: 本文遵循"署名非商业性使用相同方式共享 2.5 中国大陆"协议 您可以自由复制.发行.展览.表演.放映.广播或通 ...

  4. 自建服务器部署WEB网站可公网访问

    文章目录 自建服务器部署WEB网站可公网访问 一.公网IP端口映射到服务器主机或者内网穿透 二.Nginx 反向代理和负载均衡 2.1 windows 10 安装Nginx 2.2 ubuntu18. ...

  5. 如何快速在东方通服务器部署web项目

    一.安装JDK,配置环境变量 二.TongWeb中间件安装部署 将TongWeb中间件绿色免安装版解压后,在TongWeb根目录下放入许可证license.dat. 注意:TongWeb解压目录不要包 ...

  6. 龙芯服务器部署WEB服务的体验和详细步骤

    http://www.loongson.cn/news/company/511.html 2016年8月,通过龙芯俱乐部的<龙芯团购>(网址http://www.loongsonclub. ...

  7. 应用程序服务器和Web服务器之间有什么区别?

    应用程序服务器和Web服务器之间有什么区别? #1楼 最大的不同是Web服务器处理HTTP请求,而应用程序服务器将在任意数量的协议上执行业务逻辑. #2楼 这取决于特定的体系结构. 某些应用程序服务器 ...

  8. 如何搭建自己的本地服务器,Web服务器

    搭建本地服务器,Web服务器--保姆级教程! 本文首发于https://chens.life/How-to-build-your-own-server.html. 先上图!大致思路就是如此. 前言 暑 ...

  9. 【网络是怎么连接的】—— 1.2 向 DNS 服务器查询 Web 服务器的 IP 地址

    目录 1.2 向 DNS 服务器查询 Web 服务器的 IP 地址 1.2.1 IP地址基础知识 1.2.2 域名和 IP 地址并用的理由 1.2.3 Socket 库提供查询 IP 地址的功能 1. ...

最新文章

  1. 解读基于多传感器融合的卡尔曼滤波算法
  2. CISCO路由器安全配置
  3. 【Python教程】类及对象教程
  4. node遍历给定目录下特定文件,内容合并到一个文件
  5. django入门项目图书管理
  6. Redis的事务(一次执行多条命令,防止重读重写)
  7. spark 上下游shuffle结果的存放获取
  8. C语言课后习题(13)
  9. netty4 收不到服务器响应的数据_Netty模拟redis服务器
  10. java向hdfs提交命令_Java语言操作HDFS常用命令测试代码
  11. 为了方便手机观看,博文最好36个字一行
  12. C语言 简单的在线电子词典
  13. Windows 更新错误 0x80073712
  14. win7黑屏,提示副本不是正版的激活解决办法
  15. Unity报错:InvalidOperationException:You are tring to read lnput using the UnityEngine. ……的解决办法
  16. 勃林格殷格翰战略入股新瑞鹏,进一步拓展中国宠物市场
  17. C语言建立循环单链表并输出
  18. jmeter压力测试工具,雪崩效应,容错组件Sentinel
  19. 【HTML——盛开花朵】(效果+代码)
  20. Tensorflow faster RCNN目标检测车牌

热门文章

  1. 英语专业转学计算机,转学案例分析二十二:英语专业转经济名校michigan
  2. 通用的JS表单验证插件代码
  3. 今天OCM认证通过了
  4. 室外无线覆盖解决方案
  5. 自媒体、短视频博主都在用这5个免费视频网站
  6. SARscape数据处理SAR数据笔记——DINSAR微小形变提取
  7. stm32f103c8t6+ESP8266利用onenet平台上传数据到云平台,再利用云平台远程下发命令给单片机控制LED灯亮灭
  8. 解决在ceph对象网关中使用s3fs报错“unable to access MOUNTPOINT /s3: Transport endpoint is not connected”
  9. 南方CASS11.0.0.8最新版安装教程附下载地址
  10. DevSecOps | 极狐GitLab 动态应用程序安全测试(DAST)使用指南