Set up Linux Infrastructure using Stackscripts
Simple and applicable way to use Stackscripts
Stackscripts automate infrastructure set up. I have used them in university projects.
In my group project, 3 members of my group went to explore all kinds of providers — Azure, AWS, Heroku and Netlify were some that I remember. Owing to the structure of our project, these providers didn’t support the project or were too complex to figure out in a limited time frame (like Azure’s many features).
Under a suggestion, I decided to try out Vultr and saw that it was very easy to set up with some options to automate the set up process. While I used Vultr as the IaaS provider, there are alternatives, such as Linode and DigitalOcean that also support stackscripts.
Stackscripts are very powerful, they can automate creating users, installing necessary packages, cloning repositories and deploying. In this article I’ll go through the stackscript I used to deploy a sample Rails 6 app.
Functions and Users
First step I did is to create the functions that will be used throughout the whole script.
At the very start:
- Store the result of the stackscript in a log file (in
/root/stackscript.log
) - Include credentials the default user (
courtney
)
For the functions, there are: a log function, install essentials function and user creation.
For the apt-get
install, you may notice the -y
, this is to auto-accept installation (otherwise a confirmation prompt will appear). Alternatively you can pipe “Y” in, but it is easier to do this with the flag.
Update, Install and Create Our User
Now, let’s move on to actually creating and installing our user.
With a clean server, you will likely need to update it. This can take some time, so I did this first. Afterwards, install some of the components necessary for your project. Once installation is complete, I created my user using the previously defined function.
Note the use of the ‘log’ function, this is very helpful if the script runs into an error or stalls. In /root/stackscript.log
you can view where the script is up to.
Now let’s get ready to deploy. First install NGINX, Passenger, rbenv and clone the project.
Clone our Repository
The sample project I will use is a simple Rails6 web app that can login (link here). Because it is a Rails project, we need to install Ruby and rbenv — this will change depending on your project.
For the web server, I am using NGINX and Passenger.
At the bottom, I cloned the sample rails project as the user I created before. Note that I cloned this into /var/www/rails
(a folder which I needed to give myself ownership over through chown
).
After this, let’s complete the deployment.
Deploy
In this final step, we can set up the deployment for the application.
The final touches to our deployment! Copy the nginx settings of our project (mine is in config/deploy) into NGINX’s sites-available
. Then create a symbolic link to it in sites-enabled
.
Our site should be up now. With that, get the latest commit of the Rails project, and migrate the database.
Finally, restart NGINX to make sure it has all the updated configurations. Check your log file for “Done!” to know when it has completed successfully.
Stats
For my project, it took about 6–7 minutes for the whole stackscript to complete. Which is much faster than doing it manually.
From start to finish, this stackscript created a user, installed packages, cloned my repo and deployed it on NGINX. Hopefully this can help you to speed up your deployment process.
Complete Deployment Script
The stackscript was broken up in this article. For the full stackscript, refer to the below link.