Django SimpleCMDB WSGI

时间:2023-03-09 21:52:41
Django SimpleCMDB WSGI

一、WSGI 介绍

(1) 在前面的学习中,我们是通过 python manage.py runserver 0.0.0.0:8000 来启动并访问开发服务器的;
(2) 但在实际中我们是通过直接访问 Apache 或 Nginx 来访问开发服务器,这就需要用到 WSGI(Web Server Gateway Interface,Web服务器网关接口);
(3) WSGI 是作为 Web Server(Web服务器) 与 Web Application(Web应用程序) 之间的一种接口,实现 Web Server 与 Web Application 之间的交互;
(4) 这里的 Web Server 可以是 Apache 或 Nginx ,而 Web Application 也就是我们的 Django 项目(SimpleCMDB),通过 WSGI ,我们直接访问 Apache 或 Nginx 就能直接访问到我们的项目。

Django SimpleCMDB WSGI

二、SimpleCMDB 与 Apache 结合

[root@localhost ~]$ yum install -y mod_wsgi    # 先给 Apache 安装 WSGI 模块,Apache 是基于模块工作的
[root@localhost ~]$ cat /etc/httpd/conf.d/simplecmdb.conf    # Apache配置如下
<VirtualHost *:>
WSGIDaemonProcess simplecmdb python-path=/opt/SimpleCMDB:/usr/lib/python2./site-packages
WSGIProcessGroup simplecmdb
WSGIScriptAlias / /opt/SimpleCMDB/SimpleCMDB/wsgi.py
Alias /static /usr/lib/python2./site-packages/django/contrib/admin/static
</VirtualHost> <Directory /opt/SimpleCMDB/SimpleCMDB>
Order allow,deny
Allow from all
</Directory> WSGISocketPrefix /var/run/wsgi //关于WSGI的配置的解释说明,参考:https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html
[root@localhost ~]$ /etc/init.d/httpd start    # 启动 Apahce,这样我们就能直接通过 http://your_ip/ 访问 SimpleCMDB 项目了

三、SImpleCMDB 与 Nginx 结合

[root@localhost ~]$ pip install gunicorn    # 先给 Nginx 安装 gunicorn 模块,Nginx 是基于模块工作的
[root@localhost ~]$ cat /usr/local/nginx/conf/vhosts/simplecmdb.conf     # Nginx 配置如下
server {
listen ;
server_name www.simplecmdb.com; # 指定要代理的网站域名 location /static/admin/ { # Django 后台管理页面配置
root /usr/lib/python2./site-packages/django/contrib/admin/;
index index.html index.htm index.php;
} location / {
proxy_pass http://localhost:80; # 指定代理的网站的实际地址
}
}
[root@localhost ~]$ cd /opt/SimpleCMDB/                                 # 进入项目目录
[root@localhost SimpleCMDB]$ gunicorn SimpleCMDB.wsgi:application -D # 在后台运行WSGI
[root@localhost ~]$ /usr/local/nginx/sbin/nginx -t
[root@localhost ~]$ /usr/local/nginx/sbin/nginx -s reload
//重载 Nginx,这样我们就能直接通过 http://your_ip/ 访问 SimpleCMDB 项目了