简体   繁体   English

如何为Django项目设置apache2.conf文件?

[英]How to set the apache2.conf file for a django project?

I am now deploying a django test project on aws-ec2 and the AMI is Ubuntu18.04 with Python 3.6, Django 2.1, Apache2. 我现在在aws-ec2上部署django测试项目,AMI是Ubuntu18.04,带有Python 3.6,Django 2.1,Apache2。

The project is under /var/www/Project and I am trying to add the setting to apache.conf. 该项目在/ var / www / Project下,我正在尝试将设置添加到apache.conf中。

The project is simply generated by django-admin startproject Project and I want make sure that when hit the public IP provided by the instance, it should show up the django default page. 该项目仅由django-admin startproject Project生成,我想确保当点击实例提供的公共IP时,它应该显示django默认页面。

WSGIDaemonProcess ubuntu  processes=2 threads=12 python-path=/var/www/Project
WSGIProcessGroup ubuntu
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /var/www/Project/Project/wsgi.py

<Directory /var/www/Project/Project>
    Require all granted
</Directory>

Now i got the internal server error. 现在我收到内部服务器错误。

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

I have tried this previously. 我以前尝试过这个。 And it seems like it only works when i use python2.7 with django 1.11. 而且似乎只有在我将python2.7与Django 1.11一起使用时才有效。

WSGIScriptAlias / /var/www/Project/Project/wsgi.py

WSGIPythonPath /var/www/Project

<Directory /var/www/Project/Project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

what's wrong with my conf file? 我的conf文件怎么了?

I finally come up with the following solution. 我终于想出了以下解决方案。

If you are using Apache2, Python2 and Ubuntu18.04 LTS, you can change the apache2.conf file by adding the following: 如果您使用的是Apache2,Python2和Ubuntu18.04 LTS,则可以通过添加以下内容来更改apache2.conf文件:

WSGIScriptAlias / [path_to_wsgi.py]

WSGIPythonPath [path_to_project]

<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

If you want to use the latest Django 2+ version, change the /etc/apache2/sites-available/000-default.conf which is the settings for HTTP port 80. (000-default.conf file can't contains WSGIPythonPath in virtualhost) 如果要使用最新的Django 2+版本,请更改/etc/apache2/sites-available/000-default.conf(这是HTTP端口80的设置。)(000-default.conf文件中不能包含WSGIPythonPath)虚拟主机)

<VirtualHost *:80>
ServerAdmin webmaster@localhost
Alias /static [path_to_static_folder]

<Directory [path_to_static_folder]>
Require all granted
</Directory>

<Directory [path_to_project]>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

WSGIDaemonProcess [name] python-home=[path_to_virtualenv] python-path=[path_to_project]
WSGIProcessGroup [name]
WSGIScriptAlias / [path_to_project_wsgi.py]

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM