更换PC后Fedora 23虚拟机中Apache无法部署Django站点
Hey there, let's work through this issue you're facing with your migrated Fedora 23/Apache VM. The fact that hostname -f returns "localhost" is a big clue here—let's break down the fixes step by step:
First off, your Apache might be only binding to the loopback address (127.0.0.1), which is why non-local requests aren't getting through:
- Open your httpd.conf file with a text editor:
vi /etc/httpd/conf/httpd.conf - Look for the
Listendirective. If it's set toListen 127.0.0.1:80, change it toListen 0.0.0.0:80(this tells Apache to listen on all available network interfaces) or explicitly specify your VM's actual IP address likeListen 192.168.x.x:80 - Restart Apache to apply changes:
systemctl restart httpd.service
A hostname -f result of "localhost" means your system doesn't have a proper Fully Qualified Domain Name (FQDN) configured, which can throw off service bindings and network recognition:
- Edit
/etc/hostnameand set a meaningful FQDN, for example:fedora-django.local - Update
/etc/hoststo map your VM's IP to this new hostname. Add a line like:
Replace192.168.x.x fedora-django.local fedora-django192.168.x.xwith your VM's actual network IP address - Apply the hostname change immediately with:
hostnamectl set-hostname fedora-django.local - Verify it worked by running
hostname -fagain—it should now return your new FQDN
Django will reject requests from addresses not listed in its ALLOWED_HOSTS setting, even if Apache is listening correctly:
- Open your Django project's
settings.pyfile - Modify the
ALLOWED_HOSTSline to include your VM's IP, hostname, and localhost for testing:ALLOWED_HOSTS = ['192.168.x.x', 'fedora-django.local', 'localhost'] - Restart your Django application (or the Apache/mod_wsgi service if you're using that deployment method)
Don't forget the basics:
- Ensure your VM is using bridge network mode (this lets it get an IP on the same network as your new host computer—NAT or host-only modes can cause connectivity blocks)
- Ping your VM's IP from your new host to confirm network connectivity. If ping fails, troubleshoot your VM's network setup first
- Check Fedora's firewall (firewalld) to make sure HTTP traffic is allowed:
firewall-cmd --add-service=http --permanent firewall-cmd --reload
To answer your original question: Yes, updating /etc/hosts and httpd.conf is absolutely part of the solution, but you'll also need to address the Django settings and network/firewall checks to get everything working properly.
内容的提问来源于stack exchange,提问作者Niko

