Saturday, June 24, 2017

Running Laravel on Windows

Here I'll discuss about how we can install and run Laravel on Windows environment and start development in Laravel. 

First step is to download the Laravel package from https://github.com/laravel/laravel and Unzip it in a folder, say C:\xampp2\htdocs\laravel. Notice that my Xampp is installed in c:\xampp2.

Now we have two ways we can start the Laravel project running.

First Method :: From Command Line

A. First, create a DB through phpmyadmin, the name I gave is 'homestead'

B. Secondly, rename the file .env.example to .env file which holds all the basic configuration for running Laravel. It contains the DB configuration, application URL. We need to make changes accordingly. 
  
See that APP_KEY= is still left blank. We'll generate a key for our application shortly.

C. In this situation, if we try yo run Laravel server by issuing command "php artisan serve", it will show some "file not found" error as some dependencies are still unavailable.  



  
  We need to install them by issuing "composer install". 
  

  
D. After the command "composer install" ran, we can see that a new folder called "vendor" is created where various libraries are installed. 

E. Now, see what happens if we try to run the server by issuing "php artisan serve

    The server is run. Below message is shown in the command line .. 
   
    Laravel Development Server started: <http://127.0.0.1:8000>
   
   and if we hit the URL http://127.0.0.1:8000 in browser, see what happens
   
  


   
So, we need to follow some more steps as shown below.

F. Press ctrl + c to stop the server. Run command "php artisan key:generate" to create new Security Key.

   If the command line console shows any message like "Dotenv values containing spaces must be surrounded by quotes.", it means any settings provided in .env file need to be wrapped by a quote (") or (').
   
   

   
   See, it is reported that an Application key has been generated. The environment configuration file .env is updated also with the new key. In my case, it is as shown below :
   
 APP_KEY=base64:6Ed0zZN/3u+Q58OyZn6JnTvXjO7/vxM0Jdvder18QU8=
   
G. Now issue "php artisan serve" to start your server. Visit the webpage at http://127.0.0.1:8000.

Second Method :: The other way

A. The steps till Security Key Generation shown above are still required here. But we don't need to issue the command 'php artisan serve' here ...

Start your Xampp. In my case, I have a configuration where port 80 is listened by IIS, 8082 is listened by Xampp Apache. 

I have Xampp 3.2.2 installed on my Windows 8. In my case, Xampp is installed in c:\xampp2 folder.

Here, I'll be assigned the Laravel to a different port 8060.  

So, I just create a Virtual Host in file c:\xampp2\apache\conf\extra\httpd-vhosts.conf file as shown below.

<VirtualHost *:8060>
    DocumentRoot "C:\xampp2\htdocs\laravel\public"
    ServerName laravel.dev
</VirtualHost>

So, my Xampp will listen/serve to 8060 port as well.

If I change this 8060 to default 8082, my Xampp Home page (http://127.0.0.1:8082) will be replaced by Laravel, which I don't want.

B. Next, start the Xampp server. See below, the port numbers apache is serving now.




C. Hit "http://127.0.0.1:8060/" in the browser URL to see Laravel running.

D. You can also modify c:\windows\system32\drivers\etc\hosts file to have entry like this :
   
  127.0.0.1:8060  laravel.dev
   
   So, if you enter 'laravel.dev' in address bar of the browser, it will point to 127.0.0.1:8060.

The 'public' directory inside the "Laravel" installation contains the index.php file, which is the entry point for all requests to our  application. This directory also holds all JavaScript, images and CSS.

I hope this helps.