How to Run a Laravel-based Application using php artisan serve in a virtual server

Posted on

Introduction

This article is specifically informing how to run a Laravel-based application using a certain command. The command is ‘php artisan serve’ to start opening port for listening an incoming request. Moreover, the Laravel-based application is running in a virtual server exist in a running VirtualBox application. So, the virtual server is actually a Linux CentOS 8 running in a CLI (Command Line Interface) mode. After running the Laravel-based application, accessing it from the local virtual server will be possible only using a command line.

Steps for running Laravel-based Application

In order to demonstrate it, the following are the steps to achieve it :

  1. First of all, just access the virtual server. Accessing virtual server can be a direct access or a remote access. In the context of a remote access using SSH conection, just read the article with the title of ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’ in this link.

  2. After successfully logging in to the virtual server, just run the application. By default, the command is in the following command :

    php artisan serve

    The execution exist as follows for an example :

    [root@10 app]# php artisan serve
    Starting Laravel development server: http://127.0.0.1:8000
    [Sat Mar 20 06:33:18 2021] PHP 7.4.16 Development Server (http://127.0.0.1:8000) started
    
    
  3. After that, do not forget to add a rule in the firewall service. The rule is to allow incoming request to port 8000. So, the following is the command execution to achieve it :

    [root@10 ~]# firewall-cmd --add-port=8000/tcp --permanent
    success
    [root@10 ~]#
    
  4. Soon after, just execute the following command to reload the firewall rule  :

    [root@10 ~]# firewall-cmd --reload
    success
    [root@10 ~]#
    
    
  5. Make sure there is a Laravel-based application already exist. Read the article in this link where the title is ‘How to Create a Laravel Project using PHP Composer Command’.

  6. Next, try to access the page directly from the command line. Since it is a virtual server running in a CLI (Command Line Interface) mode, so the following is the way to check the service provided using ‘php artisan serve’ :

    [root@10 app]# curl localhost:8000
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <title>Laravel</title>
    
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    
    <!-- Styles -->
    <style>
    ...
    </style>
    
    <style>
    body {
    font-family: 'Nunito', sans-serif;
    }
    </style>
    </head>
    <body class="antialiased">
    <div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
    
    <div class="max-w-6xl mx-auto sm:px-6 lg:px-8">
    <div class="flex justify-center pt-8 sm:justify-start sm:pt-0">
    ...
    </div>
    
    <div class="mt-8 bg-white dark:bg-gray-800 overflow-hidden shadow sm:rounded-lg">
    <div class="grid grid-cols-1 md:grid-cols-2">
    <div class="p-6">
    <div class="flex items-center">
    ...
    <div class="ml-4 text-lg leading-7 font-semibold"><a href="https://laravel.com/docs" class="underline text-gray-900 dark:text-white">Documentation</a></div>
    </div>
    
    <div class="ml-12">
    <div class="mt-2 text-gray-600 dark:text-gray-400 text-sm">
    Laravel has wonderful, thorough documentation covering every aspect of the framework. Whether you are new to the framework or have previous experience with Laravel, we recommend reading all of the documentation from beginning to end.
    </div>
    </div>
    </div>
    
    <div class="p-6 border-t border-gray-200 dark:border-gray-700 md:border-t-0 md:border-l">
    <div class="flex items-center">
    ...
    <div class="ml-4 text-lg leading-7 font-semibold"><a href="https://laracasts.com" class="underline text-gray-900 dark:text-white">Laracasts</a></div>
    </div>
    
    <div class="ml-12">
    <div class="mt-2 text-gray-600 dark:text-gray-400 text-sm">
    Laracasts offers thousands of video tutorials on Laravel, PHP, and JavaScript development. Check them out, see for yourself, and massively level up your development skills in the process.
    </div>
    </div>
    </div>
    
    <div class="p-6 border-t border-gray-200 dark:border-gray-700">
    <div class="flex items-center">
    ...
    <div class="ml-4 text-lg leading-7 font-semibold"><a href="https://laravel-news.com/" class="underline text-gray-900 dark:text-white">Laravel News</a></div>
    </div>
    
    <div class="ml-12">
    <div class="mt-2 text-gray-600 dark:text-gray-400 text-sm">
    Laravel News is a community driven portal and newsletter aggregating all of the latest and most important news in the Laravel ecosystem, including new package releases and tutorials.
    </div>
    </div>
    </div>
    
    <div class="p-6 border-t border-gray-200 dark:border-gray-700 md:border-l">
    <div class="flex items-center">
    ...
    <div class="ml-4 text-lg leading-7 font-semibold text-gray-900 dark:text-white">Vibrant Ecosystem</div>
    </div>
    
    <div class="ml-12">
    <div class="mt-2 text-gray-600 dark:text-gray-400 text-sm">
    Laravel's robust library of first-party tools and libraries, such as <a href="https://forge.laravel.com" class="underline">Forge</a>, <a href="https://vapor.laravel.com" class="underline">Vapor</a>, <a href="https://nova.laravel.com" class="underline">Nova</a>, and <a href="https://envoyer.io" class="underline">Envoyer</a> help you take your projects to the next level. Pair them with powerful open source libraries like <a href="https://laravel.com/docs/billing" class="underline">Cashier</a>, <a href="https://laravel.com/docs/dusk" class="underline">Dusk</a>, <a href="https://laravel.com/docs/broadcasting" class="underline">Echo</a>, <a href="https://laravel.com/docs/horizon" class="underline">Horizon</a>, <a href="https://laravel.com/docs/sanctum" class="underline">Sanctum</a>, <a href="https://laravel.com/docs/telescope" class="underline">Telescope</a>, and more.
    </div>
    </div>
    </div>
    </div>
    </div>
    
    <div class="flex justify-center mt-4 sm:items-center sm:justify-between">
    <div class="text-center text-sm text-gray-500 sm:text-left">
    <div class="flex items-center">
    ...
    
    <a href="https://laravel.bigcartel.com" class="ml-1 underline">
    Shop
    </a>
    
    ...
    
    <a href="https://github.com/sponsors/taylorotwell" class="ml-1 underline">
    Sponsor
    </a>
    </div>
    </div>
    
    <div class="ml-4 text-center text-sm text-gray-500 sm:text-right sm:ml-0">
    Laravel v8.33.1 (PHP v7.4.16)
    </div>
    </div>
    </div>
    </div>
    </body>
    </html>
    [root@10 app]#
    

    The access is limited from the local virtual server because it is running only from the local interface of ‘127.0.0.1’. So, in order to test the service, it is only possible in the local virtual server. The parameter given in the ‘php artisan serve’ does not include any additional parameter to support listening on all available interfaces in the virtual server.

Leave a Reply