DEV Community

Nivethan
Nivethan

Posted on • Edited on • Originally published at nivethan.dev

A Web App in Rust - 16 Deploying Our Application

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.

./

hacker-clone> cargo build --release
Enter fullscreen mode Exit fullscreen mode

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!

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/
Enter fullscreen mode Exit fullscreen mode

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

hacker-release> ./hacker-clone
Enter fullscreen mode Exit fullscreen mode

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.

sudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

Once installed we can start nginx.

sudo service nginx start
Enter fullscreen mode Exit fullscreen mode

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

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;
        }
}                   
Enter fullscreen mode Exit fullscreen mode

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

sudo ln -s /etc/nginx/sites-available/hacker-clone /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

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!

sudo nginx -T
Enter fullscreen mode Exit fullscreen mode

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.

sudo service nginx restart
Enter fullscreen mode Exit fullscreen mode

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.

hacker-release> nohup ./hacker-clone > log.txt &
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)