Django 简明教程
Django - Apache Setup
到目前为止,在我们的示例中,我们使用了 Django 开发 Web 服务器。但此服务器仅用于测试,不适合生产环境。一旦进入生产环境,就需要 Apache、Nginx 等真实服务器。让我们在本章中讨论 Apache。
So far, in our examples, we have used the Django dev web server. But this server is just for testing and is not fit for production environment. Once in production, you need a real server like Apache, Nginx, etc. Let’s discuss Apache in this chapter.
通过 Apache 提供 Django 应用程序是使用 mod_wsgi 完成的。所以首先要确保已安装 Apache 和 mod_wsgi。请记住,当我们创建项目并查看项目结构时,其看起来如下 −
Serving Django applications via Apache is done by using mod_wsgi. So the first thing is to make sure you have Apache and mod_wsgi installed. Remember, when we created our project and we looked at the project structure, it looked like −
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
wsgi.py 文件负责 Django 和 Apache 之间的链接。
The wsgi.py file is the one taking care of the link between Django and Apache.
假设我们想与 Apache 共享我们的项目(myproject)。我们只需要设置 Apache 以访问我们的文件夹即可。假设我们将 myproject 文件夹放在默认的 "/var/www/html" 中。在此阶段,将通过 127.0.0.1/myproject 访问项目。这将导致 Apache 仅列出文件夹,如下图所示。
Let’s say we want to share our project (myproject) with Apache. We just need to set Apache to access our folder. Assume we put our myproject folder in the default "/var/www/html". At this stage, accessing the project will be done via 127.0.0.1/myproject. This will result in Apache just listing the folder as shown in the following snapshot.
如所见,Apache 并未处理 Django 内容。若要处理此问题,我们需要在 httpd.conf 中配置 Apache。所以打开 httpd.conf 并添加以下行 −
As seen, Apache is not handling Django stuff. For this to be taken care of, we need to configure Apache in httpd.conf. So open the httpd.conf and add the following line −
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py
WSGIPythonPath /var/www/html/myproject/
<Directory /var/www/html/myproject/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
如果您能以 127.0.0.1/myapp/connection 访问登录页面,您将会看到以下页面 −
If you can access the login page as 127.0.0.1/myapp/connection, you will get to see the following page −