How to deploy Vapor app on Ubuntu 16.04 and run it in production

This Tutorial we are going to deploy vapor app from local server to Ubuntu server and run it via supervisor and nginx .


First we have vapor app and we need to deploy it on server, the best way to deploy it is via git, so i’m using Gitlab.com to make it private, so create new project .

now click create project, and you should see different ways to add git to your app, so i’m gonna use Existing folder because already i have project

$ cd your-vapor-app-folder
$ git init
$ git remote add origin https://<your_name>@gitlab.com/<your_name>/vapor-app.git
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

now after pushing your app, you should see it on gitlab

now connect to your server using ssh, and install vapor in ubuntu, you can check vapor documentation .

now install supervisor and nginx .

$ sudo apt-get install -y supervisor nginx

so supervisor need user they can manage the app to start and stop it, so let’s create a user, and give it permissions to access vapor-app .

$ adduser <username>

you should see result like this

Adding user `myappuser' ...
Adding new group `myappuser' (1000) ...
Adding new user `myappuser' (1000) with group `myappuser' ...
Creating home directory `/home/myappuser' ...
Copying files from `/etc/skel' ...

so now let’s grant a user sudo privileges by running:

$ visudo

search for the line that looks like this:

root    ALL=(ALL:ALL) ALL

below this line, copy the format you see here, changing only the word “root” to reference the new user that you would like to give sudo privileges to:

root    ALL=(ALL:ALL) ALL
<username> ALL=(ALL:ALL) ALL

you should add a new line like this for each user that should be given full sudo privileges. When you are finished, you can save and close the file by hitting CTRL-X, followed by "Y", and then hit "ENTER" to confirm.

now give <username> permissions and added to www-data group:

$ sudo adduser <username> www-data
$ sudo chown -R www-data:www-data /home/
<username>/vapor-app
$ sudo chmod -R g+rwX /home/
<username>/vapor-app

now go to /home/<username> and install your app there by running:

$ git clone https://<your_name>@gitlab.com/<your_name>/vapor-app.git

you should see your app, now build your app to install dependencies and generate release version by running :

$ vapor build --release

after build done, now just test it if working without issues by running:

$ .build/release/Run --env=production

if you see this, that mean it’s worked perfectly 👍, now let’s add the app to supervisor .

first let’s create conf file in /etc/supervisor/conf.d, by running :

$ nano /etc/supervisor/conf.d/vapor-app.conf

and put this code inside it and change program name to your_app_name and also your app path and don’t forgot user to change it :

[program:vapor-app]
command=/home/myappuser/vapor-app/.build/release/Run serve --env=production
directory=/home/myappuser/vapor-app
user=<username>
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

now save it and run this commands :

$ supervisorctl reread
$ supervisorctl add vapor-app
$ supervisorctl start vapor-app

if supervisor service (inactive) you can active it by running :

$ sudo systemctl status supervisor
$ sudo systemctl start supervisor
$ sudo systemctl stop supervisor

if have any issue with supervisor check this link down below

https://github.com/Supervisor/supervisor/issues/480

now if you run :

$ supervisorctl status

you should see your app working on some process

vapor-app                        RUNNING   pid 3762, uptime 0:02:09

now you can access to your app through :

http://IP_SERVER:8080/


Now let’s run it via nginx

first we need to enable UFW service and allow to SSH & HTTP ports by running :

$ sudo ufw enable
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw status

now after allow to ports let’s add vapor-app server block to nginx by running :

$ sudo nano /etc/nginx/sites-available/default

and replace default block with your_app block :

server {
listen 80 default_server;
listen [::]:80 default_server;
        location / {
proxy_pass http://127.0.0.1:8080;
}
}
You can add server_name if you want run your app on another domain or sub-domain

Now save the file and restart nginx by running :

$ sudo service nginx restart

Congratulations 🎉 Now it work