Laravel in IIS

I had the need to be able to deploy a Laravel based application to an IIS server that was shared by many basic PHP applications without monopolizing the entire server instance like most deployment guides cover. I figured out that you can create an Application inside IIS, point to the /public folder in the Laravel application, and with some routing defined in a web.config you can get the application serving properly from subpath.

**This assumes you already have Laravel installed on your machine and know how to create a basic application using it**
**This also assumes you have IIS setup already to serve basic PHP 7.x applications**

Let’s get started.

Create your new application using the laravel command. I have my applications in C:/Sites on my development box.
laravel new iisTest
cd iisTest

Make the auth schaffolding to give us extra routes to visit later.
php artisan make:auth

Run npm to setup the tools and resources needed for your application (for this exercise it’s really optional)
npm install

Setup the DB connection in .env and run the migrations
php artisan migrate

Test your application real quick using the built in server
php artisan serve

Now that we have a working application to start with we can setup IIS to serve the application correctly. Start by opening up IIS and going to the Default Web Site.

Right click on Default Web Site and click Add Application

Choose the Alias for the application, ie: the subpath you want for the application from your server root.
For Physican Path, select the public folder in your application.
Application Pool can remain DefaultAppPool unless you want a separate pool for your applications.

You can verify this if you see css, js, and svg folders under the iisTest entry under Default Web Site

If you open your web browser to http://127.0.0.1/iisTest/ you should see your application is running now without the artisan serve command

However, if you click on the Login or Register links (or navigate to any known Route), you will receive an error message.

We have to setup URL Rewriting so that all URLs for routes are passed through the index.php of the Laravel application.

First, download URL Rewrite for IIS
https://www.iis.net/downloads/microsoft/url-rewrite

Then, create a web.config file inside the public folder of the application. In my case:
C:/Sites/iisTest/public/web.config

Add the following to the web.config:

These rules will pass all URLs through index.php, including GET parameters.

Test your site again and you should see that routes are now working.

One issue you may experience when using Laravel Mix is resource URLs in css for local assets will be based off the root of the server but since our application will live in a subpath from the server those resources will not be loaded properly. To fix this, we need to add one change to the webpack.mix.js file. Replace or edit the file to add the following option flag:

mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.options({
processCssUrls: false
});

*Be sure to terminate npm run watch if you have it currently running after making the change to webpack.mix.js.

This tells WebPack to leave URLs in the CSS alone so everything stays relative to the real path of the application.

*One caveat is you have to manually copy resources like images to the public folder yourself.

Now we have a Laravel based application running in IIS with working routes that is not monopolizing the server instance. This may not be the best way to do this sort of thing but I was faced with the requirement that the application reside as a subpath to the server like all the current ones.

One thought on “Laravel in IIS”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.