能否通过Laravel Valet服务部署自有生产域名并共享网站?
Absolutely! You can absolutely use Laravel Valet to deploy and share your site with a custom production domain—it’s totally doable, and here’s a step-by-step breakdown to get you set up smoothly:
1. Point Your Domain to Your Server’s Public IP
First, you need to direct internet traffic to your site’s host. Head to your domain registrar’s DNS settings and add an A record that maps your domain (e.g., yourcustomdomain.com) to the public IP address of the machine running Valet. If you’re using a subdomain like blog.yourcustomdomain.com, create an A record for that subdomain instead.
Wait for the DNS changes to propagate—this can take anywhere from a few minutes to a couple of hours, depending on your domain’s TTL settings. You can verify it’s working later with a ping yourcustomdomain.com command in your terminal.
2. Configure Valet for Your Custom Domain
Once your DNS is set, open your terminal, navigate to your Laravel project directory, and run these commands:
- Link your project directly to your custom domain:
valet link yourcustomdomain.com - Generate an SSL certificate to enable HTTPS (critical for production sites to keep traffic secure):
valet secure yourcustomdomain.com
This tells Valet to serve your project whenever someone visits your custom domain. If you manage multiple projects, valet link is more precise than valet park (which serves all projects in a directory using their folder names as domains).
3. Set Up Port Forwarding (If Hosting Locally)
If you’re running Valet on a local machine (not a dedicated server), you’ll need to configure port forwarding on your router to let external traffic reach your site:
- Log into your router’s admin panel (usually via
192.168.1.1or a similar address). - Forward ports 80 (HTTP) and 443 (HTTPS) to your local machine’s internal IP address. Make sure your local machine has a static internal IP so the forwarding doesn’t break after a reboot.
- Note: Some residential ISPs block ports 80 and 443. If that’s the case,
valet shareworks as a temporary workaround (it uses ngrok to create a public tunnel), but for long-term use with your own domain, you might need to ask your ISP to unblock those ports or switch to a VPS.
4. Verify Your Setup
After DNS propagation and port forwarding are complete, open a browser and visit https://yourcustomdomain.com—you should see your Laravel site live! If you run into issues, double-check:
- Your DNS A record is pointing to the correct IP.
- Your machine’s firewall (or router firewall) allows incoming traffic on ports 80 and 443.
- Valet is running (use
valet startif it’s stopped).
Quick Notes for Production Use
Valet is lightweight and perfect for personal projects or low-traffic production sites. For high-traffic applications, you might want to consider a more robust setup like Nginx + PHP-FPM or Laravel Forge, but Valet works great for most individual use cases. Just remember to keep Valet, Laravel, and your server’s software updated to maintain security.
内容的提问来源于stack exchange,提问作者user8000871

