“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
preface
Double Eleven is coming again, and there are a lot of activities on various cloud service platforms. I don’t know if you have got your own cloud server. This article mainly introduces the steps of a Web application from purchasing a server to launching a website. The whole text is more, write more detailed.
Domain name purchase
If you want we can application on the browser used by everyone, a domain is a must, there are many kinds of way to buy domain names, domain registrar in which there are many at home and abroad the registrar to buy is the same, the domain name is usually pay by years, here we choose nets of ali cloud, because in ali cloud buy domain name for the record will be more convenient. Ten thousand net, buy very simple, it is ok to come by step.
Server selection
This is the age of the cloud, and many companies are deploying their servers in the cloud. When we choose and buy a server, the main thing we need to consider is configuration and manufacturers, the big manufacturers are Ali Cloud, Amazon, Tencent cloud, Baidu cloud. Of course, we’d better choose domestic manufacturers, because our users are generally in China. Here we choose the host of Ali Cloud. Ali cloud host has a variety of configurations. Since we are still learning for the purpose, we just choose the cheap one.
The domain name registration
If our website wants to be accessible in China, we must put on record. If it is to buy a server in Ali Cloud, we can put on record in Ali Cloud very conveniently. To put on record, we need to provide our personal information. It should be noted that during the period of putting on record, we must ensure that our domain name is not accessible online, otherwise it will not pass.
Connecting to the server
FinalShell
In addition to using our local terminal directly to connect to the server, we can also choose other more convenient shell tools, such as FinalShell. After downloading FinalShell, click connect to input host IP, user name and password. The default user name is root, and the password is the password you input when buying the server. The password can also be changed in Ali Cloud control. After confirmation, the server is successfully accessed. FinalShell saves your password so we don’t have to enter it the second time.
Local terminal
Although it is very convenient to log in directly with FinalShell, we still need to configure the local terminal to facilitate the deployment of the application. We open the terminal and enter the IP and default user name
SSH 117.28.112.6 @ rootCopy the code
Then enter the password, the local terminal login will not automatically save the password, we need to re-enter the password every time we log in, this is very annoying, and we will need to enter the password many times every time we deploy the application to the server by command. In order to solve this problem, we need to generate a local SSH public key, and then through the local SSH public key, let the server identify our computer, so that we do not need to enter a password when logging in from this computer. SSH file (id_rsa and id_rsa.pub). SSH file (id_rsa and id_rsa.pub). SSH file (id_rsa.pub)
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Copy the code
So you have your public and private keys in your.ssh folder. Then open your server and generate the public key as well
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Copy the code
Create authorized_keys via vim and copy your local SSH public key into the authorized_keys file. Run the chmod 600 authorized_keys command to obtain the permission. Then run the sudo service SSH restart command to restart the SSH service. So we don’t need to enter a password when we log in again.
Install Nodejs and MongoDb
Nodejs
First let’s install some of the required dependencies
sudo apt-get install vim openssl build-essential libssl-dev wget curl
Copy the code
Then install NVM to manage the Node version.
Wget - qO - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bashCopy the code
Install the latest version of NodeJS using NVM
NVM install v12.13.0Copy the code
Switch to version V12.13.0. If you want to use another version of Node, you can also use this command to switch.
NVM use v12.13.0Copy the code
Set the node version used by the system by default
NVM alias default v12.13.0Copy the code
There are many ways to install NodeJS. You can also download the nodeJS package and decompress it to install it.
MongoDB
The first step is to import the public key, which is the public key used by the package management system
Wget - qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt -- the key to addCopy the code
Step 2, create a list file for MongoDB
Echo "deb [arch = amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongo - org / 4.2 multiverse" | sudo tee The/etc/apt/sources. List. D/mongo - org - 4.2. The listCopy the code
Step 3: Update the system after importing the public key
sudo apt-get update
Copy the code
Step 4, install MondoDB using app-get
sudo apt-get install -y mongodb-org
Copy the code
This completes the mongodb installation and finally starts mongodb using the service service
sudo service mongod start
Copy the code
Use the command if you need to stop or restart
sudo service mongod stop
sudo service mongod restart
Copy the code
Configure Nginx to implement direction proxy
Install nginx
Installing nginx is simple, using apt-get directly
sudo apt-get install nginx
Copy the code
/etc/nginx/conf.d/, this directory is used to store nginx configuration files, create a new file helloworld.conf, the name of the file is not important, just recognize it, nginx will load the configuration file.
Upstream {server 127.0.0.1:3333; } server {# default is listen 80; Server_name 117.28.112.6; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_pass http://helloworld; proxy_redirect off; }}Copy the code
This configuration means that 117.28.112.6:3333 is proxied to port 117.28.112.6:80. 80 is the default port, so access to http://117.28.112.6:80 can be written as http://117.28.112.6
After saving and exiting, enter the following command to test whether the configuration is correct
nginx -t
Copy the code
If the dialog box is displayed, the configuration is correct. Restart the nginx
sudo nginx -s reload
Copy the code
If we use port 3333 to start the Node application, we can directly use http://117.28.112.6 to access the website, so that we can use 117.28.112.6 to resolve the domain name
Formally deploy and release live projects to the server
We used PM2 to deploy the live project on the server, and then we uploaded the local code to the server, but it would be inefficient to upload the code from the server byte by byte every time we uploaded and changed the code, so we needed to use Git to manage the project.
First, install pM2 and Git
npm install -g pm2
sudo apt-get install git
Copy the code
Start by creating a repository on Git, and then copy the previously generated server public keys to Github. Then create file.json in the root directory, which is the automatic deployment configuration file for PM2.
{
"apps" : [{
"name" : "Test"."script" : "app.js"."env": {
"COMMON_VARIABLE": "true"
},
"env_production": {
"NODE_ENV": "production"}}]."deploy" : {
"production" : {
"user" : "root"."host" : ["110.26.112.7"].// Server IP address
"post" : "22".// Port for logging in to the server
"ref" : "origin/master".// The Git branch to deploy
"repo" : "[email protected]:HJianfeng/xingzhou.git"./ / git address
"path" : "/www/blog/production".// The project's location on the server
"ssh_options": "StrictHostKeyChecking=no".// Command to run before upload
"post-deploy" : "npm install && npm run build && pm2 startOrRestart ecosystem.json --env production"."env": {
"NODE_ENV": "production"}}}}Copy the code
The first deployment requires the server to copy the project through Git Clone, and then execute the release command on the client
pm2 deploy ecosystem.json production
Copy the code
Then you wait for the response. The biggest advantage of using configuration files is that when we need to change the code, we just need to upload Git and then execute the publish command to update our website.
Adding an SSL Certificate
There are many platforms that can apply for free SSL certificates, such as Youpaiyun, Tencent Cloud, Ali Cloud and so on. Since our server and domain name belong to Ali Cloud, we directly apply for SSL certificate on Ali Cloud, which is the SSL certificate of Ali Cloud.
After applying for a certificate according to the instructions, the certificate will be issued in 24 hours. After the certificate is issued, the nginx version is downloaded and uploaded to the server. Then configure the following code into your.conf file
listen 443; Server_name www.jsclub.top; SSL on; # open SSL ssl_certificate/WWW/SSL/www.jsclub.top.pem; # certificate file address ssl_certificate_key/WWW/SSL/www.jsclub.top.key; Ssl_session_timeout 5m; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:! ADH:! EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; if($ssl_protocol = '') { rewrite ^(.*) https://$host$1 permanent; }Copy the code
The HTTP request is then converted to HTTPS
server {
listen 80;
server_name www.jsclub.top;
return 301 https://www.jsclub.top$request_uri;
}
Copy the code
Finally, remember to check whether the security group of Ali Cloud console has increased access to port 443.