[英]How to setup django without running server
我试图从远程服务器访问 django 网站。 我在settings.py
中允许所有PC
ALLOWED_HOSTS = ['*', 'localhost']
并运行python manage.py runserver 0.0.0.0:8085
我可以从连接的计算机访问该站点。 但我需要在访问站点之前在服务器系统上运行runserver
commanad。 有没有办法在不运行命令的情况下访问网站
有很多方法可以部署 Django 但我不知道一个简单的方法:-)
对于所有方式,您至少需要一件事:网络服务器。
常见的网络服务器是 Apache2 和 nginx。
我假设您使用基于 debian 的服务器(例如 Ubuntu)。
如果您选择 apache2,请安装 apache:
sudo apt install apache2 libapache2-mod-wsgi
使用主机信息创建文件。
/etc/apache2/sites-available/foo.conf
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName {{ project_name }}.com
ServerAlias www.{{ project_name }}.com
# 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 }}/static_root/
#<Directory {{ project_directory }}/static_root>
# Options -Indexes
# Order deny,allow
# Allow from all
#</Directory>
# 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 }}/media/
#<Directory {{ project_directory }}/media>
# Options -Indexes
# Order deny,allow
# Allow from all
#</Directory>
# 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 }} threads=1 python-path={{ project_directory }}/:{{ path_to_virtualenv}}/lib/python{{ python_version }}/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 }}
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/{{ project_name }}_error.log
CustomLog ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>
代替:
运行 bash 命令:
sudo a2ensite foo.conf
sudo service apache2 restart
如果您有不起作用的静态文件,请确保在 settings.py 中正确配置它:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]
然后编辑您的 foo.conf 并再次重新启动 apache。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.