A Web App in Rust - 16 Deploying Our Application
Tags: rust, tutorial, webdev, actixweb
Published: 2020-10-25 14:42:30

Welcome back! We're finally here. The end point. Let's just steam right into deploying this little website we made.

Let's kill our cargo watch as we don't need the constant recompiles anymore!

Next, let's build our release.

./
``` bash
hacker-clone> cargo build --release
```

This will build the release version of our application.

This may take some time, feel free to watch cargo compile everything and bask in the knowledge that we finally finished something!

Once we have our application build, we now have a binary in ./target/release/ called hacker-clone.

This binary is our application. If we do ./hacker-clone, our binary would actually run. Isn't that amazing?! 

We still need our templates folder and we need our .env file but besides that, that's it. Our application went from a huge amount of work to this single binary that we can drop somewhere and run.

### Deployment

The idea of deploying our application somewhere is that we'll run our application server on localhost, 127.0.0.1, port 8000 still. Then we'll use nginx as our actual web server and pass through all requests to port 8000. This way we can server static files through nginx and server everything else through our application. 

So let's create a new directory on our machine called hacker-release. We'll then copy our release build, ./hacker-clone/target/release/hackerclone to our hacker-release folder. We'll also copy over our templates folder and our .env file as it contains our database.

If we were really deploying our application, we would need to harden our secret_key and recreate our database. 

For now we'll just pretend!

``` bash
hacker-release> ls -la
total 15744
drwxr-xr-x 1 nivethan nivethan      512 Oct 21 22:16 ./
drwxrwxrwx 1 nivethan nivethan      512 Oct 21 22:15 ../
-rw-r--r-- 1 nivethan nivethan      168 Oct 21 22:16 .env
-rwxr-xr-x 1 nivethan nivethan 15063920 Oct 21 22:16 hacker-clone*
drwxr-xr-x 1 nivethan nivethan      512 Oct 21 22:16 templates/
```

Now we should be able to do ./hacker-clone to start up our server.

``` bash
hacker-release> ./hacker-clone
```

We should be able to navigate to 127.0.0.1:8000 and see our familiar friend, the index page!

We can now set up nginx to begin proxy passing requests to our application. This is done on Windows Subsystem for Linux so there may be differences.

### Intstalling nginx

First let's install nginx.

``` bash
sudo apt-get install nginx
```

Once installed we can start nginx.

``` bash
sudo service nginx start
```

Now if we have nginx installed properly we should be able to go to 127.0.0.1:80 and see the Welcome to nginx page.

### Configuring nginx

Now we can work on wiring our application to nginx. The first step is to write our hacker-clone nginx file.

/etc/nginx/sites-available/hacker-clone
``` bash
server {
        listen 8080;
        server_name localhost;

        location / {
                proxy_pass http://localhost:8000;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }
}                   
```

Next we need to symlink from our sites-available directory to the nginx sites-enabled directory.

``` bash
sudo ln -s /etc/nginx/sites-available/hacker-clone /etc/nginx/sites-enabled/
```

One thing to note, is symlinking, is lowercase s, then source, then destination. I don't think I've ever rememebered!

Let's make sure our nginx configuration is valid, otherwise we might take down our nginx server and it won't come back up!

``` bash
sudo nginx -T
```

If we have any errors, nginx will now tell us, otherwise we'll have the entire nginx configuration printed on the screen.

Now we can restart nginx.

``` bash
sudo service nginx restart
```

Now if we navigate to 127.0.0.1:8080 we should be able to see our index page again! (If our release binary is still running!)

This doesn't seem like a big deal but it is, this is because our application is now accessed through nginx. This means that we can use nginx to server as our web server and our application will strictly be our application!

Now before we finish up there is one last thing to do.

Let's kill our hacker-clone application as it's currently running in the foreground and is logging everything to the screen. Instead we'll run it in the background and log everything to a file.

``` bash
hacker-release> nohup ./hacker-clone > log.txt &
```

The "&" makes our application run in the background, the ">" sends the output of our application to a file and the nohup makes it so that we can close our terminal session and our application will continue to run.

And with that we are done!

See you in the conclusion where we can go over things and say good bye!