-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Hosting Nancy with Nginx on Ubuntu
This tutorial describes how to install and run a NancyFx powered website on Ubuntu 12.04
Go to the mono download page to retrieve the the latest mono version. In our case, we build the latest source from github:
$ sudo apt-get install git autoconf automake libtool g++ gettext
$ mkdir ~/src
$ cd ~/src
$ git clone git://github.com/mono/mono.git
$ cd mono
$ ./autogen.sh --prefix=/usr/local
$ make get-monolite-latest
$ make
$ sudo make install
Now we are ready to run .NET applications under linux.
Go to mono to retrieve the the latest mono version for windows. In our case 3.0.2.
Open VS2012 and start a new console app NancyDemo.sln
Edit the program.cs file and add the following code:
namespace NancyDemo
{
class Program
{
static void Main(string[] args)
{
var uri = "http://localhost:8888";
Console.WriteLine(uri);
// initialize an instance of NancyHost (found in the Nancy.Hosting.Self package)
var host = new NancyHost(new Uri(uri));
host.Start(); // start hosting
//Under mono if you deamonize a process a Console.ReadLine with cause an EOF
//so we need to block another way
if (args.Any(s => s.Equals("-d", StringComparison.CurrentCultureIgnoreCase)))
{
while (true) Thread.Sleep(10000000);
}
else
{
Console.ReadKey();
}
host.Stop(); // stop hosting
}
}
}
Create a new folder Modules and add the class HelloModule.cs
namespace NancyDemo
{
public class HelloModule : NancyModule
{
public HelloModule()
{
Get["/"] = parameters => "Hello World";
}
}
}
To make sure all code at leasts builds under mono, we build the sln with xbuild.
D:\Development\Mono\NancyDemo>"C:\Program Files (x86)\Mono-3.0.2\bin\xbuild.bat" NancyDemo.sln
Once the build succeeds copy NancyDemo.exe, Nancy.dll and Nancy.Hosting.Self.dll to the linux machine. We can test if it works by running the console app.
$ mono NancyDemo.exe
Now open a browser and go to http://localhost:8888. We should see "Hello World".
nginx is the webserver we're using. We configure it to forward all requests to the nancy self hosted application. The content folder with static files will be handled by nginx.
$ sudo apt-get install nginx
Create the website configuration file in /etc/nginx/sites-available/nancydemo with the following content. The server_name is the domain on which the request will be handled. Change this to your own value.
server {
listen 80;
server_name yourdomainname.com;
root /var/www/nancydemo;
location /Content/ {
alias /var/www/nancydemo/Content/;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf)$ {
expires 365d;
}
}
location / {
proxy_pass http://127.0.0.1:8888;
}
}
To enable the website, create a symbolic link from the sites-available to the sites-enabled folder. This will make it easy to temporary disable sites in the future.
$ sudo ln -s /etc/nginx/sites-available/nancydemo /etc/nginx/sites-enabled/nancydemo
The configuration is completed, reload Nginx to apply.
$ sudo /etc/init.d/nginx reload
To make sure our nancy self hosted website never stops, we use supervisor. This program makes sure that NancyDemo keeps running.
$ apt-get install supervisor
Configure supervisor by creating a new file /etc/supervisor/conf.d/nancydemo.conf
[program:nancydemo]
command=/usr/local/bin/mono NancyDemo.exe -d
user=www-data
stderr_logfile = /var/log/supervisor/nancydemo-err.log
stdout_logfile = /var/log/supervisor/nancydemo-stdout.log
directory=/var/www/nancydemo/
And start the control manager of supervisor
$ sudo supervisorctl
And update the configuration. You should see that there is a new process added. Now start nancydemo.
$ supervisor>update
$ supervisor>start nancydemo
Go to http://yourdomain.com and see our baby saying "Hello World".
Linux is very critical about uppercase and lowercase letters, so if a view can't be found, make sure you've used the exact name with the exact uppercase and lowercase letters.
- Introduction
- Exploring the Nancy module
- Routing
- Taking a look at the DynamicDictionary
- Async
- View Engines
- Using Models
- Managing static content
- Authentication
- Lifecycle of a Nancy Application
- Bootstrapper
- Adding a custom FavIcon
- Diagnostics
- Generating a custom error page
- Localization
- SSL Behind Proxy
- Testing your application
- The cryptography helpers
- Validation
- Hosting Nancy with ASP.NET
- Hosting Nancy with WCF
- Hosting Nancy with Azure
- Hosting Nancy with Suave.IO
- Hosting Nancy with OWIN
- Hosting Nancy with Umbraco
- Hosting Nancy with Nginx on Ubuntu
- Hosting Nancy with FastCgi
- Self Hosting Nancy
- Implementing a Host
- Accessing the client certificate when using SSL
- Running Nancy on your Raspberry Pi
- Running Nancy with ASP.NET Core 3.1