Set port for php artisan.php serve
Setting a Custom Port for php artisan serve
🚀
So, you want to access different development sites on your localhost, each running on a different port? No worries! In this post, I'll walk you through the process of setting a custom port for php artisan serve
command using some easy solutions. Let's dive in! 💻
The default port 🌐
By default, when you run the php artisan serve
command, Laravel serves your application on localhost:8000
. However, sometimes, you may need to use a different port to avoid conflicts or meet specific requirements.
Solution 1: Specify the port directly 🏁
You can simply specify the desired port when running the php artisan serve
command by appending the --port
flag followed by the port number. For example, to serve your application on localhost:8080
, run the following command:
php artisan serve --port=8080
That's it! 🎉 Your Laravel application will now be accessible at localhost:8080
.
Solution 2: Use the built-in server configuration ⚙️
If you frequently need to serve your application on a specific port, you can modify the default server configuration provided by Laravel. To do this, open the ./config/serve.php
file in your Laravel project.
Within this file, you'll find an array that specifies the default options for php artisan serve
. Locate the port
key and update its value to your desired port number. For example:
<?php
return [
'port' => 8080,
];
Save the changes, and now every time you run php artisan serve
, Laravel will automatically use the port you specified in the configuration file.
Solution 3: Using the .env
file 📁
If you prefer a more flexible approach and don't want to modify the server configuration, you can utilize your .env
file. Open the .env
file in your Laravel project and add the following line:
APP_URL=http://localhost:8080
Save the changes and run the php artisan serve
command. Laravel will pick up the APP_URL
value from the .env
file, so your application will be served on the specified port.
Wrapping up 🎀
Congrats! You now know how to set a custom port for php artisan serve
in Laravel. Whether you prefer specifying the port directly, modifying the server configuration, or using the .env
file, you have different options to choose from based on your needs.
Now, go ahead and start serving your Laravel applications on the desired ports! 🚀 If you have any questions or run into any issues, feel free to leave a comment below. Happy coding! 😄