简体   繁体   中英

How to setup django without running server

i was trying to access django website from a remote server. I allowed all PC's in settings.py

ALLOWED_HOSTS = ['*', 'localhost']

And run python manage.py runserver 0.0.0.0:8085 I can access the site from connected computers. But i need to run the runserver commanad on server system before accessing site. Is there a way to access the website without running the command

There are many ways to deploy Django but i don't know an easy method:-)

For all ways you need at least one thing: The webserver.

Common webservers are Apache2 and nginx.

I assume you use a debian based server (eg. Ubuntu).

If you choose apache2, install apache with:

sudo apt install apache2 libapache2-mod-wsgi

Create the file with the hosts informations.

/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>

Replace:

  • {{ project_name }} with the name of the project
  • {{ project_directory }} with the directory path of your project
  • {{ path_to_virtualenv}} with the path to your virtualenv
  • {{ python_version }} with the pythonversion of your virtualenv

Run bash commands:

sudo a2ensite foo.conf
sudo service apache2 restart

If you have staticfiles which are not working, make sure you configured it correctly in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]

then edit your foo.conf and restart apache again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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