- This topic has 0 replies, 1 voice, and was last updated 6 months, 3 weeks ago by Chinomnso.
- November 28, 2019 at 3:55 pm #81257Participant@chinomnso
Some Laravel newbies find it hard to go through the very first step of using Laravel – installing it on their local machine. This article teaches how to install Laravel on a Windows machine, although the principles can be applied to other systems. I’m writing this article assuming that Visual Studio Code is the text editor you will be using.
XAMPP comes with Apache, MySQL, PHP and Perl. This way, you have Apache as the web server, MySQL for the database on which Laravel will run, and a PHP interpreter. Go to apachefriends.org and download the latest version of XAMPP. Installing XAMPP is a pretty straightforward process, so I ‘m not covering it here. However, if you have any port issues that make it impossible for XAMPP to function properly, be sure to check if any other apps are using ports 80 and 3306. XAMPP runs Apache and MySQL respectively on these ports. So, you need to free them up if they are being used by other apps.
Download Composer, a PHP dependency manager from composer.org and run the .exe file. Some have described Composer as “NPM for PHP”. So I hope that explains what composer would do fo you as you use Laravel. During the installation, you will be prompted to supply the path to PHP on your computer. On a Windows machine running XAMPP, it is most likely going to be C:\xampp\php\php.exe. Click Next, ignore the proxy section of the setup and proceed. Composer is now installed and is ready to work on your system.
Install Visual Studio Code
Download and install Visual Studio Code, a lightweight text editor from code.visualstudio.com. You’ll be using terminal commands a lot while working with Laravel, and you don’t want to be hopping between your terminal and Visual Sode Code. Visual Studio Code has an integrated terminal that uses Power Shell, but we will be using Git Bash.
Download git from https://git-scm.com/downloads and install. In Adjusting your PATH environment section, choose the last option. For the remaining options, choose the defaults.
Launch Visual Studio. Go to View > Integrated Terminal. You will notice it is using Windows PowerShell. Locate the Settings file by going to File > Preferences > Settings, or by clicking CTRL + , (comma). It is a JSON file, so don’t expect to see any buttons or other UI elements you can interact with. Using the search feature, search for “terminal”. Replace the path to PowerShell with the path to Git Bash. Save the file, then close your integrated terminal, reopen it and you will notice that it has changed from PowerShell to Git Bash.
Assuming you are in the root folder of your machine (C:\), use the following command to migrate to the directory where your Laravel files will be:1cd xampp/htdocs/
Now you’re in the htdocs/ directory, it is now time to download Laravel to your machine. Remember, we installed Composer. We will use it to handle the download. Here’s the command we will use:1composer create-project laravel/laravel project_name
Under normal circumstances, this should take around 2 minutes to complete. Using the following command in your terminal, migrate into the directory created by the previous command that also downloaded the Laravel files:1cd project_name
Set up a virtual host
Head over to your browser, and try loading http://localhost/project_name. You should see a page listing all the files in your project directory. That certainly is not what you want. In fact, this poses a huge security risk if left as is. If you click public/, you should see the default Laravel homepage with navigation in the centre of the page. This again, is not what you want.
It’s now time to create a virtual host. To do that, we need to edit two files. Head over to C:\xampp\apache\conf\extra\. Open the httpd-vhosts.conf file with a text editor. Copy the following code and paste it at the end of the file:123456789<virtualhost *:80>DocumentRoot "C:/xampp/htdocs/"ServerName localhost</virtualhost><virtualhost *:80>DocumentRoot "C:/xampp/htdocs/project_name/public"ServerName project_name.dev</virtualhost>
The code above creates a faux domain name and points requests to the “public” folder of your application. Be sure to change the “project_name” to the actual name of your project, and “devs” to whatever you want as a domain extension. Using a text editor again, navigate to C:\Windows\System32\drivers\etc. Open the hosts file. Copy the following code and paste at the end of the file:12127.0.0.1 localhost127.0.0.1 project_name.dev
The code above routes all requests to the “domain name” you created back to localhost, ensuring that your browser does not take the request to the internet. Save the file and restart Apache by opening your XAMPP control panel, stopping the Apache service and starting it again. Head over to your browser and enter http://localhost/project_name.dev. This will be the root of your application. If this were live on the web, no one will be able to see your file structure as we saw before.
At this point, Laravel is all set up and ready to run. Go ahead and play around with the file system, classes, namespaces, models, views, controllers and routes.
- You must be logged in to reply to this topic.