Some time ago saw zhang Yunlong’s article a programmer’s growth road – analysis of others, summed up their own, there is such a paragraph

Off-stack technology refers to the upstream and downstream of technologies in the stack, and relevant expertise outside the field, including but not limited to server technology, operation and maintenance, CDN, testing, even UI design, product design, etc. Expand the surrounding fields of technologies in your stack, and fully understand how your work is in the whole technology RESEARCH and development system. Put in an extra effort outside of work to continuously incorporate other off-stack technologies into your knowledge system and build off-stack capabilities. The depth of what you want to do on the front end often involves caching, template rendering, user experience, etc. Without a significant amount of off-stack technology, it’s hard to get your team to say enough.

Think of their time in the company, are basically write business, do front-end related work, but for other aspects is dabbled in less, or basic no. Again, when I was in the interview, the interviewer would ask some off-stack questions, such as how your company’s code was released and deployed. At this time, I was completely confused. This made me want to know how to deploy code, and here’s how to do it, for the record.

Cloud server

Cloud server purchase, can choose Ali cloud and Tencent cloud, I did not carefully to compare the price of the two, thinking is used to play how to deploy, also did not care about these, after all, it is not a long-term purchase, spend a few money, chose Tencent cloud. As to the server of choose and buy what configuration, saw individual. After the purchase, some certification and so on, need to wait a day or two. Right, I still bought a domain name, but the procedure that discovers to put on record later is quite troublesome, think to use IP address to also can visit later, did not go tube.

SSH login

Once you have a server, you need to log in. So how do you log in? Here’s a simple way to do it.

SSH public keys are generated locally

First, you need to make sure you already have the secret key. By default, a user’s SSH key is stored in the ~/. SSH directory. By going to this directory and listing its contents, you can quickly check if you have your secret key:

#local
$ cd ~/.ssh
$ ls
config      id_rsa      id_rsa.pub  known_hosts
Copy the code

We need to find a pair of files named id_dsa or ID_rsa, one of which has a.pub extension. The.pub file is your public key and the other is your private key. If you can’t find such files or if there is no.ssh directory at all, you can create them by running the ssh-keyGen program.

#local
$ ssh-keygen
Generating public/private rsa key pair.
#Enter the enter key
Enter file in which to save the key (/Users/laohan/.ssh/id_rsa):
Created directory '/Users/laohan/.ssh'.
#Enter your password twiceEnter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/laohan/.ssh/id_rsa. Your public key has been saved in /Users/laohan/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XhI9aeGsVJklGyUTvNu+6ABzOZdZL2+y5aMOVQa+ZvI laohan@bogon The key's randomart image is: +---[RSA 2048]----+ | .O*+ | | =+X . | | o O.o o | | . =.= = | | o S *o* . | | = =.*.o | | o .. E + | | . o.*. | | .o.=o.. | +----[SHA256]-----+Copy the code

First, ssh-keyGen will confirm where the key is stored (the default is.ssh/id_rsa). Then it will ask you to repeat the password twice. You can leave it blank if you don’t want to enter it when using the public key.

The public key looks like this:

$ cat ~/.ssh/id_rsa.pubssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDc4xbJxlMTgiE13I1RL6IsG44+3CQc8Ft03eZnYfNrPVeQIf9r9kTuArSiUnY+BHXn3mrQ5i+5AGi+alys94Pum2dZ 68QtlY1QdEl4iN3LFXUkbJc+M0rllaDGH5JNtfk5imVqDo8Tn7aJsFd4IXbwrl3Euf+ccOb+s92RHwzbSRx37tP9pLF9ujfL0UXfg3+DmRJMJT7iN3OiJxfu F5k8KSySEz+YbhQoNeySuvVPeRHG/U6xOGcpzNjQIPApGsuFdLT5R/5x15W7SrC//XWuIQMmlVTW2X0YH+5NjT0nlLVWxS4drtRCS66JXtRceVqs5H5InbsL fALfTPyIkZ4t laohan@bogonCopy the code

Generating a new SSH key and adding it to the SSH-agent

Cloud Server Configuration

$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
Copy the code

Next, add the developer’s SSH public key to the authorized_keys file for this user.

$ vim .ssh/authorized_keys
#Add the previously generated SSH public key to the end of the file
Copy the code

Local Config Configuration

It is now possible to log in to the cloud server using SSH without a password

ssh root@IP
# IP indicates the IP address of the cloud server
Copy the code

As above, does it feel very convenient? But in this way, you still need to know the IP address of the cloud server, so is there a less brain-saving way?

Yes, add the following code to the config file under ~/. SSH

Host hostname # hostname specifies the alias you want to set
HostName IP # IP indicates the IP address of the cloud server
User root
IdentitiesOnly yes
Copy the code

After that, you can log in to the cloud server as follows

ssh hostname # hostname is the name you just set
Copy the code

code

Personal understanding, the cloud server is a machine, your code in their own computer how to run, to the cloud server or how to run, but the running environment is different, need to do some configuration.

Now that we have a cloud server, the next step is to deploy code on the server that we can run locally.

I adopted a relatively simple way, that is, directly push my local code to Github, and then use Git clone on the server to download the code to the local. Github address koa-demo. Or you can package your code locally and use SCP for remote replication.

Cloud server setup nginx

Nginx is something that operations and maintenance guys deal with a lot. You’ve probably heard the terms reverse proxy, load balancing, etc., all related to Nginx.

Install nginx

My server is centos system, so the installation method is as follows

yum install -y nginx
Copy the code

Find out how to install other systems.

The conf configuration

For nginx, the main configuration file is nginx.cof, which is located in /etc/nginx

cd /etc/nginx
vi nginx.conf
Copy the code

The modified content is as follows

This is done so that if you need to add multiple servers later, they will be changed in the nginx.conf file, which will not be easy to maintain. Next, add the nginx-related files that we need this time

cd /etc/nginx/conf.d
touch koa-demo.conf
vi koa-demo.conf
Copy the code

Then, copy the following content to koa-demo.con and save it

Upstream koa-demo {server 127.0.0.1:3000; } server { listen 80; location / { proxy_pass http://koa-demo; }}Copy the code

After that, use the following command

Nginx -s reload # is used to test if your configuration file is okCopy the code

Not surprisingly, type your cloud server IP address into your browser at this point to see the effect. Wait, you said your browser wasn’t responding. Don’t panic, it’s because your code isn’t running yet

# My code clone is here, your path may be different from mine
cd /opt/koa-demo
npm start
Copy the code

Refresh the page again at this time, ok, done.

Next, you might want to add a chicken leg to celebrate, then quit the cloud and go to dinner. Come back after dinner, refresh browser again, I go, how no content again. Why is that?

This is because you are not opening it correctly, which means you are not starting the code correctly. If you look at the packagek.json content, NPM start actually executes node index.js. When you exit here, the process is over, think about whether it’s the same in your local area, you run on your terminal, open the browser, no problem, exit the terminal, refresh the page, no problem.

So how can we solve this problem? Well, if you’ve ever worked with node, you’ve probably heard the word Pm2 before. Yes, that’s right, and then we’re going to use this pM2 to solve our problem.

Pm2 runs the program

How to learn PM2, the fastest way to see its official website, inside the document or write very good, a Quick Start direct entry.

I won’t go into it much here, but it will be quicker to read the documentation. You can see that the script in package.json also has a command called pm2. This is the command I added earlier to start the program with pm2. The details are as follows:

Log in to the cloud server first
ssh hostname
Enter the code directory
cd /opt/koa-demo
# run
npm run pm2
Copy the code

At this point, even if you quit, refresh the page again, there will be no problem.

Docker deployment

Docker, you’ve probably heard of this term before. If you haven’t, you’re a little behind The Times. If you have not learned about Docker, please refer to teacher Ruan Yifeng’s Introduction to Docker tutorial.

Next, let’s see how to deploy our NodeJS application using Docker.

First, we need to add Dockerfile and.dockerIgnore to our KOA-demo. I’ve added it in the code, and you can simply copy it into your project. There will be no explanation as to what those commands mean. After reading Mr. Ruan’s article, you know something about it. Or you can use docker COMMAND -h to get the use of each COMMAND.

Docker is installed on cloud hosts

First of all, we need to install Docker on the cloud host. You can refer to the Docker official website About Docker CE, which has the installation instructions About each system, which is very detailed.

Compile the mirror

Go to our code directory, use the docker build command to compile the image files we need, and then use the docker run to run the container

cd /opt/koa-demo
docker build -t koa-demo .
docker run -d -p 7000:3000 koa-demo
Copy the code

Here, I’m mapping the container’s port 3000 with the local port 7000.

At this point, you can use the command to see if it works correctly

curl -i localhost:7000

# should return the following result
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 30
Date: Mon, 02 Jul 2018 12:12:26 GMT
Connection: keep-alive

It's success, congratulation!
Copy the code

Now that our container is running, you may want to try typing IP :port directly into the browser to see if it can be accessed. After you type it in, you will find that there is no response.

Why? Don’t worry, we haven’t added the nginx configuration yet.

cd /etc/nginx/conf.d
vi docker-demo.conf

# Write the followingUpstream docker-demo {server 127.0.0.1:7000; } server { listen 7000; location / { proxy_pass http://docker-demo; }}Copy the code

Use nginx -t to test if your conf is ok, and then nginx -s reload to reload.

At this point, you can use IP:Port (where Port is the Port of the previous LISTEN).

If, at this time, you are still unable to access, it may be a problem with your security group Settings.

conclusion

Above is a small white bitter deployment road, hard journey, but learned a lot of things, a lot of harvest.

Discuss the address

Welcome to the discussion, github address from scratch to learn deployment

The resources

Nginx reverse proxy and load balancing Dockerizing a node. js Web app Get Docker CE for CentOS Docker — from start to practice