Intro There are many ways to run servers and plenty of cloud platforms to do so. To start this tutorial series I am going to go with managing an EC2 server using AWS (Amazon Web Services), which is down to the metal and probably the most common way to run Node.js applications. This tutorial series is intended to help get a better understanding of cloud based servers, though there will be small parts showing how to set up a simple Node.js app. I find that most tutorials for configuring AWS or cloud servers often expect quite a bit of knowledge and can be quite daunting. For the most part there is quite a bit of reading between the lines to get through. I hope that this is quite simple to follow. To make it as simple as possible I will be writing it with Mac users in mind but if you are using Linux then it won’t be much of an issue, we will just be using bash and a text editor. This part will cover starting an AWS server SSH into your server installing Node.js creating a public HTTP endpoint that responds with a static message Creating and starting the server If you haven’t created an AWS account, open , and then choose . Follow the steps to get an account setup. https://aws.amazon.com/ Create an AWS Account Once logged into your AWS account we are going to create an EC2 machine. EC2 stands for The compute cloud is just a load of computers in Amazon’s datacenters that are connected to the internet and can be controlled by using the AWS dashboard. We are going to take one of those machines and set it up for our use. Amazon Elastic Compute Cloud. Click on the navigation bar and select EC2, which is under the category. Click . You will be presented with a bunch of options which are various different images. An image is an exact copy of a hard drive that can be easily loaded onto an empty hard drive, in this case they are being used as presets to get your machine setup easily. Without at least an operating system and SSH, it wouldn’t be possible to even configure the instance so some preset software is necessary. services compute launch instance Selecting an image The options include different versions of Windows and Linux. Most servers run Linux and it is a great choice for developing Node.js, Windows servers are only really useful for specialized applications such as .NET *shudder*. For the basic things we will be doing in this tutorial, there isn’t much difference between the Linux images. I chose to go with because it is widely used and has tonnes of guides and plenty of questions and answers on Stack Overflow. Ubuntu Server Once an image has been selected, we need to select an instance type. Notice that this is a , with . Virtual means that although it will seem like we are connecting to and configuring one computer, in fact Amazon will be running multiple instances on the same machine while pretending it isn’t, which is great for scaling and pricing. Let’s choose which is eligible for the , so if your account is less than 12 months old you can run your server for free. Thanks Amazon! virtual server virtual CPUs t2.micro free tier Selecting an instance type After you have selected your instance type, click . The following page is more complicated but you can ignore most of it for now, we may explore these options later. Next: Configure Instance Details Click . The default is 8GB of an SSD which is fine for us. Click . We don’t need any tags but they are useful for when you have a large number of instances and you need to filter and search through them. Next: Add Storage Next: Add Tags Click . A security group is a config for your server, telling it which ports it should expose to which IP addresses for certain types of traffic. Name the group something meaningful, I chose . Next: Configure Security Group tutorial group To run our app we are going to need SSH access, which by default is on port 22 and uses the TCP protocol. Amazon adds this in for us by default. There is a warning about the source being which allows us to SSH from any IP address however this is fine. If you were deploying and configuring your app from a VPN or company network then you could limit SSH access only for that IP which is a good security precaution when running something in production, but SSH access requires the correct SSH key so it is completely secure without this measure. 0.0.0.0/0 Since we would like to also serve an app we need to expose a HTTP port publicly, by default this is port 80 (but browsers strip this so you don’t see it in URLs). Click and select the type as , the default settings for this will use TCP as the protocol and expose port 80 to all IPs. Add Rule HTTP Configuring a security group To launch the instance, click then click . You will be prompted to setup an SSH key which will give you access to the instance. Choose , and name the key, I named it “tutorial”. Click . This should download a .pem file which can be used to SSH into the server. Keep this file safe because anyone can connect to your server using it, if you lose the file you will need to generate a new one. Review and Launch, Launch create a new key pair Download Key Pair Click . Click . Woo! Your instance should be booting up. Once the is then you are ready to SSH in. Launch Instance View Instances Instance State Running Remember to stop the instance when you aren’t running it! To do that just right click on the instance, under click . Note: Instance State Stop Stopping an instance SSH into your server Generally, the correct place to put your file is in your folder, in your user directory. The folder is a hidden folder, to open it in finder open terminal and execute the command. .pem .ssh .ssh open # The open command will open the# given path using the default system# application for the file type. For# folders it uses finder!$ open ~/.ssh Once the file is in your ssh folder, use to set permissions so that it can be used as a key. .pem chmod $ chmod 400 ~/.ssh/whatever-your-key-name-is.pem To SSH we need to have a username, an address and a key. The address is available when we click on our instance in the EC2 instances dashboard. Instance details The instance I setup has a public IP which I could use to connect, giving the file as a key using the flag. 52.214.64.31 .pem -i # Don't actually run this command$ ssh -i ~/.ssh/whatever-your-key-name-is.pem 52.214.64.3 However it is more correct to use the public DNS (the only real difference though is when you are connecting from EC2 machine to EC2 machine then the DNS will resolve to the private IP rather than the public IP). # Don't run this command either$ ssh -i ~/.ssh/whatever-your-key-name-is.pem ec2-52-214-64-31.eu-west-1.compute.amazonaws.com Almost there. By default, connecting to your instance without a username will try to login as which is generally not allowed. By right clicking your instance and selecting we can see what the correct username is. This is dependant on the image you chose, for ubuntu the username is by default . You can see the username in the section of the dialogue, it is the part before the symbol. root connect ubuntu Example @ Finding the SSH username To connect, the SSH command should look something like # Fill in your specific details for this to work.$ ssh -i ~/.ssh/whatever-your-key-name-is.pem ubuntu@ec2-52-214-64-31.eu-west-1.compute.amazonaws.com This should connect you to your instance, just type when prompted so that you can add your instance as a known host. You’re connected! yes Installing node and system dependencies Once in an SSH session the first thing to do is get Node.js. is a pretty great way to install Node.js and allows you to easily switch versions if required. NVM (Node Version Manager) To install NVM just run this command (same as in the NVM installation instructions). $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash This command pulls down a script from a remote URL and runs it. You now have NVM! But if you run you will notice it isn’t found. This is because NVM adds some code to your . This file is a special file that is run every time you log in to your instance, so to get NVM running you could logout and login again. However you can just run the file manually by using the command. nvm ls ~/.bashrc source $ source ~/.bashrc Now running works! But there aren’t any node versions installed! To get the latest version, just . nvm ls nvm install <latest version number> $ nvm install 7 To check node is ready to go just echo the version. $ node --version Installing node Node.js is installed! Creating a public HTTP endpoint Before concluding this part of the tutorial we are going to make a public URL anyone can request from the browser to get a response from the server. Make a directory for the server and into it. cd $ mkdir server$ cd server Now you are in your server directory, you need to npm init $ npm init This will create a package.json file which will be used to track any dependencies we use. will ask for a load of info, I tend to just press to use all the defaults. npm init enter All we will need to run our server is the package. To install and add it to . express express package.json $ npm install express --save-dev Note that now you should have a directory and node_modules package.json $ ls Now we just need to add some code to run the server. We will use nano to write the server in an file. index.js $ nano index.js The server I used just responds with “HEY!” when you do a request to . I listen to requests on port 3000. / const express = require('express')const app = express() app.get('/', (req, res) => {res.send('HEY!')}) app.listen(3000, () => console.log('Server running on port 3000')) Press to exit, ensuring you save when you exit by pressing followed by . ctrl+x y enter Now we can use node to start the server! $ node index.js Once listening, this should log “Server running on port 3000”. You may have noticed however we didn’t open our server traffic to port , we opened it to port . Port is a privileged port and running the server there using Node.js is unusual, generally using a router is better. If you change the file to use and then run you will notice you get a permission denied error. 3000 80 80 index.js 80 node index.js In the next tutorial we will go through adding a router to use port , but for now let’s just open up port so we can test our server. 80 3000 Leave your server running and go to the tab in the EC2 console. Right click the security group you setup and click . Click . This time we are going to use a custom TCP rule on port 3000, open to anywhere. Security Groups edit inbound rules Add Rule Opening up port 3000 to TCP traffic Click Save. We should now have access to our server! Using a browser, visit your public DNS URL with port 3000 and you should see the response. HEY! The response from your server To leave the server running when we log out, we need to press to pause the process (this only works when your server is running, ). When you press you will be presented with all jobs, in this case the only one there will be the Node.js job that was paused. ctrl+z node index.js ctrl+z You can see that the job number for is 1 (as noted by ). To run that in the background, use the command. node index.js [1]+ bg $ bg %1 Then logout $ exit You should still be able to access your URL and see the “HEY!” response. Conclusion If everything went well we should have a simple Node.js app which is accessible from a public URL anywhere in the world! To stop the instance just navigate to the tab in the EC2 dashboard, right click on your instance and in click . Instances Instance State Stop In the next tutorial I intend to cover running the app on port 80 using nginx using PM2 to keep the app running after a restart using PM2 to deploy the app from a local directory Let me know in the comments section if there is anything I missed or is a bit confusing! Part 2 is now available.