How to host a laravel website on windows server?
The computer where I want to host the site developed in laravel is on another network. I put the whole site on the server and ran the command
php artisan serve --host MEUIP --port MEUPORTO
When I test on another computer the browser replies that the page took a long time to respond ? What is the correct procedure to host the laravel website on another machine ?
Thank you.
1 answers
You probably want to use IIS being Windows Server, in case you may have installed one of these programs:
- IIs "default"
- IIS Express
- WebPlatformInstaller
If it is standard IIS or IIS Express
If you are using WebPlatformInstaller do not do this following procedure.
First install PHP:
Download at http://windows.php.net/download /
And install in the folder %SYSTEMROOT%\php
(examples c:\php
or d:\php
)
Steps to install PHP on standard IIs
After installing PHP, navigate to %windir%\System32\inetsrv\config\
and then edit applicationHost.config
and then add something like (editing the existing locations inside the config):
<defaultDocument enabled="true">
<files>
<add value="index.php" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
</files>
</defaultDocument>
<fastCgi>
<application
fullPath="C:\php\php-cgi.exe"
monitorChangesTo="C:\php\php.ini"
activityTimeout="300"
requestTimeout="300"
instanceMaxRequests="10000"
>
<environmentVariables>
<environmentVariable name="PHPRC" value="C:\php\" />
<environmentVariable name="PHP_FCGI_MAX_REQUESTS" value="10000" />
</environmentVariables>
</application>
</fastCgi>
And then add this inside handlers
:
<handlers accessPolicy="Read, Script">
<add
name="PHP_via_FastCGI"
path="*.php"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:\php\php-cgi.exe"
resourceType="Either"
/>
Once installed type this in run: %SYSTEMROOT%\inetpub\wwwroot
and you can install Laravel inside it, it can be a folder or directly in the folder wwwroot
.
Steps to install PHP no IIS Express
Download
- IIS Express: https://www.microsoft.com/en-US/download/details.aspx?id=48264
- PHP Windows: http://windows.php.net/download /
Installing PHP and IIS Express
Install PHP in a folder like C:\php
Swap fullPath='"C:\php\php-cgi.exe"'
and scriptProcessor='"C:\php\php-cgi.exe"
for the path that made the installation
If you have installed IIs x64:
cd C:\Program Files\IIS Express\
If you have installed IIS x86:
cd C:\Program Files (x86)\IIS Express\
Then type:
appcmd set config /section:system.webServer/fastCGI /+[fullPath='"C:\php\php-cgi.exe"']
appcmd set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='"C:\php\php-cgi.exe"',resourceType='Unspecified']
Editing or applicationhost.config
If you have repeated entries as for PHP_via_FastCGI
cd %userprofile%\Documents\IISExpress\config
notepad.exe applicationhost.config
Or using SublimeText or notepad++ or another editor select something like File > Open File
%userprofile%\Documents\IISExpress\config\applicationhost.config
And look for repeated:
<add name="PHP_via_FastCGI" path="*.php"
Installing PHP with WebPlatformInstaller
This is the only one that does not follow the above steps, as the installation is almost all automated and the My see the easiest way of all
Download
Download WebPlatformInstaller:
Installing IIs
-
Select IIs-Webserver:
-
Enter " php " and install the desired versions except Express, for example:
-
And type "rewrite":
If you notice a message like this:
Then then click on Register new PHP version
and navigate to the folder where you installed PHP, after selecting it should look like this:
All suggestions here are for any application written in PHP, not only Laravel
Configuring Laravel
After placing laravel in the desired folder you must move contents of the folder of your project to the "root" folder of your IIS (if it is standard IIs it should be wwwroot
) and create inside the folder a file named web.config
with this content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Laravel Force public">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="public/{R:1}" />
</rule>
<rule name="Laravel Routes" stopProcessing="true">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<match url="^" ignoreCase="false" />
<action type="Rewrite" url="public/index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Does not exist
Laravel in production environment
Note that when using laravel for production you must turn off debugging by changing to .env
, then change this:
APP_ENV=local
APP_DEBUG=true
By this:
APP_ENV=production
APP_DEBUG=false
Sources: