没有子域名的django应用程序部署

没有子域名的django应用程序部署

我已经将一个 django 应用部署到我拥有的服务器上。假设它的 ip 是192.168.1.1。因此我能够使用 访问那里的任何网站192.168.1.1/my_site

我现在已经部署了Django应用程序,这是一个小型 API。我正在关注教程和使用带有 mod_wsgi 和守护进程的 apache。现在的问题是,当我在本地部署时,我能够通过它访问它,localhost但由于文件的更改,所有其他站点都无法访问000-default-000.conf

当我在服务器上部署时,是否可以通过 IP 访问并且不会与其他项目冲突/var/www/html

答案1

虽然不是Django + Apache专家,但从网上找到了这个,看看它是否适合你。

django 项目

<VirtualHost *:80>
    # This is name based virtual hosting. So place an appropriate server name
    #   here. Example: django.devsrv.local
    ServerName  [[SERVER_NAME]]
    ServerAdmin webmaster@localhost

    # This alias makes serving static files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /static/  {{ project_directory }}/run/static/

    # This alias makes serving media files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /media/  {{ project_directory }}/run/media/

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias /   {{ project_directory }}/{{ project_name }}/wsgi.py

    # PROCESS_NAME specifies a distinct name of this process
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
    # PATH/TO/PROJECT_ROOT is the full path to your project's root directory, 
    #   containing your project files
    # PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
    #   path to its directory.
    #   Generally you must specify the path to Python's site-packages.
    WSGIDaemonProcess   {{ project_name }}  python-path={{ project_directory }}:{{ project_directory }}/../lib/python2.7/site-packages

    # PROCESS_GROUP specifies a distinct name for the process group
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
    WSGIProcessGroup    {{ project_name }}

    # Serving static files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/static>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    # Serving media files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/media>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

    # PROJECT_NAME is used to seperate the log files of this application
    ErrorLog    ${APACHE_LOG_DIR}/{{ project_name }}_error.log
    CustomLog   ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>

金属蟾蜍

<VirtualHost *:80> 
 ServerName mysite.example.com 
 DocumentRoot /var/www/vhosts/mysite 
 WSGIScriptAlias / /var/www/vhosts/mysite/myproject/wsgi.py 

 # adjust the following line to match your Python path 
 WSGIDaemonProcess mysite.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mysite/venv/lib/python3.5 
 WSGIProcessGroup mysite.example.com 

 <directory /var/www/vhosts/mysite> 
   AllowOverride all 
   Require all granted 
   Options FollowSymlinks 
 </directory> 

 Alias /static/ /var/www/vhosts/mysite/static/ 

 <Directory /var/www/vhosts/mysite/static> 
  Require all granted 
 </Directory> 
</VirtualHost> 

Metal Toad 进一步向您展示如何调整您的wsgi.py以使其与以下设备配合良好apache

"""
exposes the WSGI callable as a module-level variable named ``application``. 
For more information on this file, see 
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ 
""" 
import os 
import time 
import traceback 
import signal 
import sys 

from django.core.wsgi import get_wsgi_application 

sys.path.append('/var/www/vhosts/mysite') 
# adjust the Python version in the line below as needed 
sys.path.append('/var/www/vhosts/mysite/venv/lib/python3.5/site-packages') 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") 

try: 
    application = get_wsgi_application() 
except Exception: 
    # Error loading applications 
    if 'mod_wsgi' in sys.modules: 
        traceback.print_exc() 
        os.kill(os.getpid(), signal.SIGINT) 
        time.sleep(2.5) 

所以,不妨试用一下,让它为你服务。Metal Toad 表示,你甚至可以设置多个网站:

第二个虚拟主机的设置与第一个非常相似。主要区别在于服务器名称、路径和 wsgi.py 文件的位置。

相关内容