HowTo: Linux, Nginx, Mono, Asp.Net Mvc

These are notes I made for getting an application written for Asp.Net Mvc (It was probably Mvc 3-ish) to run on a Centos server.

Note that some of the issues/solutions may vary across Linux variants. The interface Nginx-Mono is FastCGI, which they both support. The Mono webserver is xsp4.

Of course, running cross platform is all much easier these days if you use .Net Core and Kestrel 🙂 And een for Net Framework running on Mono, the work done since Microsoft bought Xamarin has been really helpful for e.g. Mvc compatibility.

The Reading List

The Checklist

Ensure nginx has read execute permissions on your application directory and all parent directories

nginx conf section for webapp

With the Asp.Net website listening on port 9001

#
server {
listen 8080;
server_name your-public-facing-nginx-server-name.com;

# use /smoketest/ to confirm that nginx is reading this config 
# and has read/execute access to the 
# /usr/share/nginx directories and files:

location /smoketest/ {
root /usr/share/nginx/smoketestredir;
index index.html;
}

# pass XSP to FastCGI server listening on 127.0.0.1:9001
# use a different port for each ASP.NET site you create
# (port 9000 is often taken by PHP on default webserver setups)

location / {

# replace fastcgi with this to confirm nginx read address
# to files in root /usr/share/nginx/mywebapplication;
fastcgi_pass  127.0.0.1:9001;
fastcgi_index index.html;

include /etc/nginx/fastcgi_params;
fastcgi_param  PATH_INFO          "";
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}

Systemd Startup Script

vim /etc/systemd/system/fastcgi-mono-hdmbreas.service

[Unit]
Description=FastCgi-mono-server4 for Application
After=syslog.target

[Service]
Type=simple
ExecStart=/bin/fastcgi-mono-server4 --applications /:/usr/share/nginx/hdmbreas/hdm-fe-web --socket=tcp:127.0.0.1:9001
Restart=always

systemctl daemon-reload
systemctl start fastcgi-mono-app.service
systemctl status fastcgi-mono-app.service
systemctl status fastcgi-mono-app.service

Error: [error] 7216#0: *119 upstream sent too big header while reading response header

  • Considering setting the buffer size
  • Considering setting the busy buffer size
  • e.g. nginx config: fastcgi_buffers 16 16k; fastcgi_buffer_size 32k;

Watching Errors

Watch errors with tail -F /var/log/nginx/error.log

Mono MVC System.UnauthorizedAccessException Access to the path “/Library/Frameworks/Mono.framework/Versions/…/etc/mono/registry” is denied

Is an error you are likely to see if you run a Visual Studio MVC template in Mono. There are two options for fixing it.

  • Do this from the command line:
    sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
    sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry

    (replacing 3.2.5 with your mono version, which you get at the command line with mono --version)

  • Or delete the reference to Microsoft.Web.Infrastructure.dll from the project and delete it from the bin directory too.

The important difference is that deleting Microsoft.Web.Infrastructure.dll will stop your project working on .Net, so the registry access is simpler for cross-platformers. Another option for cross-platform project files would be something like this in the .csproj file:

<Target Name="AfterBuild">
    <Delete Files="$(WebProjectOutputDir)\bin\Microsoft.Web.Infrastructure.dll" Condition=" '$(OS)' != 'Windows_NT'" />
  </Target>

I prefer the 'grant access to the registry approach' myself but it does mean having to re-run the 2 line script for every new version of mono.

Compile & Build Mono on Mac OS X

In spite of what you might still read on http://www.mono-project.com/, mono source moved to github.com/mono/mono. To build and compile on the mac however the simplest instructions are still the "One Stop Shop Build Script" at the bottom of http://www.mono-project.com/Compiling_Mono_on_OSX. They worked for me first time although I hit a couple of issues:

  1. Having a space in the path to where I built it broke the build script
  2. Fetching the mono repo from git failed several times. This may - or may not - be related to the OS X 10.9 Mavericks / Versions issue noted at http://www.git-tower.com/blog/make-git-rebase-safe-on-osx/ but I've had no further problems since following their instruction to
    git config --global core.trustctime false

Asp.Net MVC4 C# Razor Template for Mono on Mac and Linux

Is available on www.github.com/chrisfcarroll/AspNetTemplatesForMono/Mvc4CSharpRazorFx45Intranet. (The github download includes all the required dlls in case you don't have NuGet working, so it's an oversized download. If you do have NuGet working, you'll find everything as NuGet expects).

One step to get the MVC4 template working on Mono

Download from github and open the solution in Xamarin Studio. It should almost work out-of-the-box; the one thing you have to do is either do this from the command line:

sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry

(replacing 3.2.5 with your mono version, which you get at the command line with mono --version);
Or, the alternative to this is to delete the reference to Microsoft.Web.Infrastructure.dll from the project and delete it from the bin directory too.
Small disclaimer: tested on Mac only. If you can offer feedback for Linux/Windows that would be great.

Footnote: How to build the Visual Studio Asp.Net MVC 4 template on Mono

For the interested who have a copy of VS2012, here's the recipe.

  1. From Visual Studio 2012 create a new Project with the C#-Web-MVC4 Wizard and choose Intranet-Razor-No Unit Test.
  2. Try to run the resulting project on Mono on Mac.
  3. Address the problems arising in the following order
    1. System.UnauthorizedAccessException

      Access to the path "/Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry" is denied. (Where the whole path varies by O/S & 3.2.5 is your mono version).

      Resolution

      sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
      sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry

      i.e. allow members of admin group to read/write the registry. Obviously you could widen privileges but this is all that's needed for asp.net.

    2. System.TypeLoadException

      Could not load type 'System.Data.Entity.Migrations.Sql.SqlCeMigrationSqlGenerator' from assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

      Resolution

      Remove references to Entity framework from THREE places: web.config, Project references AND delete them from the bin directory if they in there:

      • System.Data.Entity
      • System.Web.Entity
      • Entity.Framework
    3. System.TypeLoadException

      Could not load type 'System.Net.Http.MultipartRelatedStreamProvider' from assembly 'System.Net.Http.Formatting ...

      Resolution

      add this redirect to the web.config. You should find there are other 'dependentAssembly' redirects in there within the section so put it alongside them:

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
          <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
        </dependentAssembly>
    4. And finally ... System.Web.HttpException

      Unauthorized.
      Because you don't have windows authentication available to you when running on Mac or Linux.

      Resolution

      Comment out

      <deny users="?"></deny>

      from the web.config.
      Note that you're now unsecured. The simplest way to add security back in is to use Forms authentication. If you don't know how to do this, the simplest start might to look at http://google.com/search?q=Forms+authentication+user+names+in+web.config

Kudos