简介
Django框架:
Django内部集成有web开发中的界面渲染,地址路由等功能,在实际开发中可以方便快捷的开发web应用并且可以采用Python语言进行开发。
uwsgi平台:
由于在服务器运行程序时,我们不能够像本地调试时对代码直接运行,为提高效率一般采用后台多进程运行,便于更多的人来访问,可以理解为远端后台运行的平台。
Nginx代理:
当代码运行起来的时候我们往往是运行在本地环路127.0.0.1或其他本地地址,当我们让代码跑起来之后需要使用Nginx设置端口代理将数据在内部环路与外网之间相互转发使外网可以完全访问并进行数据交互。
一、Django框架简单配置使用
正常web应用流程: web头生成(声明即将展示的文本类型等相关信息)、HTML文本渲染(在HTML相关位置保证文本的动态性)、路由(将访问时的请求返回指定的内容)
1、setting.py配置
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True# 输出相关错误信息,当界面只显示错误500等可能是这里没有打开
ALLOWED_HOSTS = ['*']# 可以运行的地址,*表示所有IP均可访问
# 静态文件地址配置
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
2、urls.py路由配置
urlpatterns = [
# path('admin/', admin.site.urls),
path(r'login/',login),# 'login/'为网页上http://127.0.0.1:8000/login
# 后面的login表示定义的login函数,定义格式def login(request):,其中request为用户请求数据
# 一般request使用方法:request.GET.get(),request.POST.get()
]
3、相关函数配置
def login(request):
request.GET.getlist()
request.POST.getlist()
return render(request, 'result.html', {
'public_subject':动态内容,
'subject':subject,
'professional':professional})
4、文本渲染
静态文件引入
<head>
<meta charset="utf-8">
<title>check_subject</title>
<link href="../static/tit.css" rel="stylesheet" type="text/css">
</head>
<form method="POST" action="/login/">
{% for msg in msgs %}<br>
<br>{{ msg.0 }}、{{ msg.1 }}<br>
{% if msg.2 != null %}
<input type="checkbox" value={{ msg.2 }} name={{ msg.1 }}>
{{ msg.2 }}
{% endif %}
{% if msg.3 != null %}
<input type="checkbox" value={{ msg.3 }} name={{ msg.1 }}>
{{ msg.3 }}
{% endif %}
{% endfor %}
<br><br>
<input type="submit" value="开始筛选" >
<br>
<h2>{{ input_Null }}</h2>
{% for sigle_professional in professional %}
{% if forloop.counter != 1 %}
、
{% endif %}
{{ sigle_professional }}
{% endfor %}
二、uwsgi平台使用
- 安装uwsgi:
yum install uwsgi
- 查找安装位置并建立软连接
#查找安装位置一般为/usr/local/bin/uwsgi
find / -name uwsgi
#建立软连接
ln -r uwsgi /usr/bin/uwsgi
- 建立配置文件uwsgi.ini
#在Django项目根目录(与manage.py同级)建立文件
touch uwsgi.ini
如图位置:
配置内容
[uwsgi]
#本地访问用户名需要在系统中注册否则默认以登录名进行访问
uid=Webusr1
gid=Webusr
#配置Nginx时使用socket否则使用http,此处使用http若使用Nginx代理时
http=127.0.0.1:8000
#项目根目录
chdir=/home/Django_2
#wsgi.py文件路径
wsgi-file=/home/Django_2/Django_2/wsgi.py
module=Django_2.wsgi:application
callable=apip
#最大进程数为4,最大线程数为2
processes=4
thread=2
master=True
#将进程号写入uwsgi.pid文件,运行中产生的运行日志写入run.log文件
pidfile=uwsgi.pid
daemonize=run.log
- 使用uwsgi运行Django项目在本地127.0.0.1:8000端口上
uwsgi --ini uwsgi.ini
如下图则运行正常:
- 停止运行uwsgi
#正常通过进程号进行停止
uwsgi --stop uwsgi.pid
#强制停止
pkill -f uwsgi -9
#重启运行
uwsgi --reload uwsgi.pid
三、Nginx代理使用
- 安装
yum install wget #安装下载工具
wget http://www.atomicorp.com/installers/atomic #下载
sh ./atomic #安装
yum check-update #更新yum源 有的需要更新几次 才有nginx最新版本 否则是老版本nginx
- 配置Nginx
#移动到相关目录下
cd /etc/nginx/
#备份配置文件nginx.conf
cp nginx.conf nginx.conf.default
#修改配置适合代理
vi nginx.conf
Nginx.conf代理配置
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# 主要修改位置location中的proxy_pass
#需要添加上刚刚uwsgi运行在的内环地址
location / {
proxy_pass http://127.0.0.1:8000;
root login;#根地址
index login.html index.html H1.html;#首页名称
}
# 静态文件采用代理的方式进行发送,方便增删本地的静态文件
location ~ .*\.(js|css|jpg|png)$ {
proxy_pass http://127.0.0.1:8000;
}
# 404与500界面不顺眼可以自行设计修改此处location后面的本地地址
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# https协议相关设置
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}