background
I just bought Ali cloud server, now want to deploy node.js code to Ali cloud server above; I wrote an article before how to buy and log in ali cloud server, not clear students can learn from oh, juejin.cn/post/684490…
Log in to ali Cloud server
- We log in to the web version of Ali Cloud server, currently we use the root account
- First obtain the public key on the local computer, using the command line:
cat ~/.ssh/id_rsa.pub
- Copy the public key and change it to:
Echo 'public key' >> ~/. SSH /authorized_keys
- Put the above content do not know the web version of Ali Cloud server command line;
- After executing the replication command, you can log in to ali cloud server on this machine.
- Open the local terminal and enter
SSH root@Public IP address
- If we never remember the IP address, we can set the local host so that we can use our own name to access it later
- To exit the cloud machine, type Exit
- How to prevent SSH from stalling, you can run the following code in this machine (note not Ali cloud server)
echo "Host *" >> /etc/ssh/ssh_config
echo " ServerAliveInterval 30" >> /etc/ssh/ssh_config
Copy the code
Create a new account
- As for why we need to create a new account, because the root account is too high, if the root account is controlled, it is equivalent to the whole machine is controlled, so we generally do not use the root account;
- Log in to aliyun server locally as root and enter ‘adduser username’.
- I’m gonna create a.ssh directory,
mkdir /home/dj/.ssh
- Copy the authorized_keys file from the server’s.ssh folder to the newly created.ssh folder
cp ~/.ssh/authorized_keys /home/dj/.ssh/
- Make authorized_keys readable and executable
chmod 755 /home/dj/.ssh/authorized_keys
- Set the owner of the file to the new user
chown dj:dj /home/dj/.ssh/authorized_keys
- Once the above Settings are complete, you can log out and log in as a new user
Add sudo permission to the new user
- Our new user’s permission is too small, sometimes we need root’s permission, then root can add sudo to the new user.
adduser dj sudo
- So what is sudo? Sudo stands for super user do; Windows-like running as an administrator; Normally, we should not use root account, but DJ account. In case of special operation, we add sudo in front of it, and please exit root. The password that needs to be entered is DJ’s password, not root’s password.
- Command execution is tricky
ctrol + a
I can quickly go back to the command line,sudo !!
Execute the last command using sudo
Install Node.js on the server
curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
sudo sed -i 's/deb.nodesource.com\/node_8.x/mirrors.tuna.tsinghua.edu.cn\/nodesource\/deb_8.x/g' /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
sudo apt-get install -y nodejs
sudo apt install git
The deployment of application
- Git clone xxx. git
- Using the command line
touch log
Create a Log file - Run the Node program
node server.js 8888 > log 2>&1
This will return a number. This number is the process ID, which can be used if we want to kill the processkill -9 id
- We can make the startup command into a script
echo 'node server.js 8888 > log 2>&1 &' >> ./start
- Give start an executable permission
chmod +x ./start
- run
./start
perform
Access the server code in the native browser
- Since the node server code listens to port 8888, we need to add port 8888 to the security policy in Ali cloud server first
- Configuration is complete, you customer in the browser to access: http://47.92.100.195:8888/
Conclusion:
Thanks to fangfang’s selfless sharing in the Hungry People Valley, the income is quite a lot!