Django 2.2.8在虚拟主机与VPS上配置失败,访问显示空白页
Hey there, let’s work through figuring out why you’re seeing a blank page after setting up Django 2.2.8 on your server. I’ve tackled similar deployment headaches before, so let’s break this down step by step:
1. Enable Debug Mode & Check Error Logs
First, temporarily set DEBUG = True in your project’s settings.py—this will replace the blank page with detailed error messages (just remember to switch it back off for production!). If you still don’t see errors, dig into your server’s logs:
- For Apache: Check
/var/log/apache2/error.log - For Nginx: Look at
/var/log/nginx/error.log - On shared hosting, your provider probably has a dedicated log section in their control panel
Also, double-check your ALLOWED_HOSTS setting includes your domain or server IP, like:
ALLOWED_HOSTS = ['your-domain.com', '123.45.67.89']
2. Validate Your WSGI Configuration
Since you followed a shared hosting guide, the WSGI setup is likely where things went wrong. Verify these key points:
- Make sure your
wsgi.pyfile points to your virtual environment’s Python interpreter. Add this at the top if it’s missing:
activate_this = '/path/to/your/python_files/venv/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this))
- Confirm the WSGI file correctly loads your project settings. The line should look like:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_site.settings')
- For Apache, check your
.htaccessfile to ensure it routes requests to the WSGI script properly. A typical entry might be:
RewriteEngine On RewriteRule ^(.*)$ /path/to/wsgi.py/$1 [L]
3. Fix File Permissions
Django needs read access to your project files and write access to folders like static or media:
- Set the web server user (usually
www-datafor Apache,nginxfor Nginx) as the owner of your project:
sudo chown -R www-data:www-data /path/to/your_site
- Apply proper directory and file permissions:
find /path/to/your_site -type d -exec chmod 755 {} \; find /path/to/your_site -type f -exec chmod 644 {} \;
- If you have a
staticdirectory, runpython manage.py collectstatic(ensureSTATIC_ROOTis correctly defined insettings.pyfirst).
4. Test Django Locally on the Server
Connect via SSH and run the Django dev server to check if the project works directly:
cd /path/to/your_site source /path/to/python_files/venv/bin/activate python manage.py runserver 0.0.0.0:8000
Then access http://your-server-ip:8000 from your local machine (make sure port 8000 is open in your firewall). If this works, the issue is with your web server config—not Django itself.
5. Recheck Installed Dependencies
Even if you ran pip install -r requirements.txt, some packages might have failed to install. Reactivate your virtual environment and verify:
source /path/to/python_files/venv/bin/activate pip list
Confirm Django==2.2.8 and all other required packages (like psycopg2 for PostgreSQL) are listed. Reinstall any missing ones with pip install package-name.
内容的提问来源于stack exchange,提问作者Tayyab Vohra

