# 05. Web Server > [!NOTE] > **Attribution:** Adapted from [*NixOS in Production*](https://github.com/Gabriella439/nixos-in-production) by Gabriella Gonzalez ([CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/)). > Support the author by purchasing the official edition on [Leanpub](https://leanpub.com/nixos-in-production). --- Now that we can build and run a local NixOS virtual machine we can create our first toy web server. We will use this server throughout this book as the running example which will start off simple and slowly grow in maturity as we increase the realism of the example and build out the supporting infrastructure. ## Hello, world! We'll begin from the template project from "Setting up your development environment". You can either begin from the previous chapter by running the following command (if you haven't done so already): ```bash $ nix flake init --template 'github:Gabriella439/nixos-in-production/0.10#setup' ``` … or if you want to skip straight to the final result at the end of this chapter you can run: ```bash $ nix flake init --template 'github:Gabriella439/nixos-in-production/0.10#server' ``` Let's modify `module.nix` to specify a machine that serves a simple static "Hello, world!" page on `http://localhost`: ```nix # module.nix { pkgs, ... }: { services = { getty.autologinUser = "root"; nginx = { enable = true; virtualHosts.localhost.locations."/" = { index = "index.html"; root = pkgs.writeTextDir "index.html" ''
Hello, world! ''; }; }; }; networking.firewall.allowedTCPPorts = [ 80 ]; virtualisation.forwardPorts = [ { from = "host"; guest.port = 80; host.port = 8080; } ]; system.stateVersion = "23.11"; } ``` You can read the above code as saying: - *Enable `nginx` which currently only listens on `localhost`* In other words, `nginx` will only respond to requests addressed to `localhost` (e.g. `127.0.0.1`). - *Serve a static web page* … which is a bare-bones "Hello, world!" HTML page. - *Open port 80 on the virtual machine's firewall* … since that is the port that `nginx` will listen on by default until we create a certificate and enable TLS. - *Forward port 8080 on the "host" to port 80 on the "guest"* The "guest" is the virtual machine and the "host" is your development machine. - *Allow the `root` user to log in with an empty password* - *Set the system's "state version" to 23.11* > [!NOTE] > You always want to specify a system state version that matches the starting version of Nixpkgs for that machine and *never change it* afterwards. In other words, even if you upgrade Nixpkgs later on you would keep the state version the same. > > Nixpkgs uses the state version to migrate your NixOS system because in order to migrate your system each migration needs to know where your system started from. > > Two common mistakes NixOS users sometimes make are: > > - *updating the state version when they upgrade Nixpkgs* > > This will cause the machine to never be migrated because Nixpkgs will think that the machine was never deployed to an older version. > > > - *specifying a uniform state version across a fleet of NixOS machines* > > For example, you might have one NixOS machine in your data center that was first deployed using Nixpkgs 23.05 and another machine in your data center that was first deployed using Nixpkgs 23.11. If you try to change their state versions to match then one or the other might not upgrade correctly. If you deploy that using `nix run` you can open the web page in your browser by visiting [`http://localhost:8080`](http://localhost:8080) which should display the following contents: > Hello, world! > [!WARNING] > In general I don't recommend testing things by hand like this. Remember the "master cue": > > > Every common build/test/deploy-related activity should be possible with at most a single command using Nix’s command line interface. > > In a later chapter we'll cover how to automate this sort of testing using NixOS's support for integration tests. These tests will also take care of starting up and tearing down the virtual machine for you so that you don't have to do that by hand either. ## DevOps The previous example illustrates how NixOS promotes DevOps on a small scale. If the inline web page represents the software development half of the project (the "Dev") and the `nginx` configuration represents the operational half of the project (the "Ops") then we can in principle store both the "Dev" and the "Ops" halves of our project within the same file. As an extreme example, we can even template the web page with system configuration options! ```nix # module.nix { config, lib, pkgs, ... }: { services = { getty.autologinUser = "root"; nginx = { enable = true; virtualHosts.localhost.locations."/" = { index = "index.html"; root = pkgs.writeTextDir "index.html" '' This server's firewall has the following open ports: