Published on

How to run Laravel on custom port

Authors

Running Laravel on a different port than the default port (usually port 80 for HTTP and port 443 for HTTPS) can be useful in various scenarios, such as running multiple Laravel applications simultaneously on the same server or testing your application in a development environment. Here's how you can run Laravel on a different port:

  1. Using Laravel's Built-in Server:

    Laravel comes with a built-in server that you can use for development purposes. You can specify the port number when starting the server using the --port option.

    php artisan serve --port=8000
    

    This command will start the Laravel development server on port 8000. You can replace 8000 with any available port number you prefer.

  2. Using Homestead or Valet:

    If you're using Laravel Homestead or Laravel Valet for local development, you can configure the port number in the Homestead or Valet configuration files.

    For Homestead, you can specify the port number in the Homestead.yaml file:

    ports:
      - send: 50000
        to: 5000
    

    This example maps port 50000 on the host machine to port 5000 on the guest machine, where your Laravel application will be running.

    For Valet, you can specify the port number using the valet share command:

    valet share --port=8000
    

    This command will expose your Laravel application publicly with the specified port number.

  3. Using Apache or Nginx:

    If you're using Apache or Nginx to serve your Laravel application, you can configure the virtual host or server block to listen on a specific port.

    For Apache, you can modify the VirtualHost directive in your Apache configuration file (httpd.conf or a separate virtual host file) to include the desired port:

    <VirtualHost *:8080>
        ...
    </VirtualHost>
    

    For Nginx, you can modify the server block configuration in your Nginx configuration file (usually located in /etc/nginx/sites-available/) to listen on a specific port:

    server {
        listen 8080;
        ...
    }
    

    After making these changes, don't forget to restart Apache or Nginx for the changes to take effect.

  4. Using Docker:

    If you're running your Laravel application within a Docker container, you can expose ports in your Dockerfile or Docker Compose configuration.

    In your Dockerfile:

    EXPOSE 8000
    

    Or in your Docker Compose configuration:

    ports:
      - '8000:80'
    

    This will map port 8000 on the host machine to port 80 in the Docker container where your Laravel application is running.

Choose the method that best suits your development or deployment environment, and don't forget to ensure that the chosen port is not already in use by another application.