Some experience with VPS running node. js

Source: Internet
Author: User

VPS system Selection of the system installation difficult to easily compare

Ubuntu, Debian is simpler, CentOS is a bit cumbersome, 32-bit systems are more memory-saving than 64-bit

Digitalocean even launched Ubuntu + node. JS One-click Deployment, within a minute (officially known as 55 seconds) will be able to set up the environment (note: Through this link registration, the account will get 10 knives, and the minimum Plan 5 knives/month, you can use for 2 months free)

Note: It is strongly recommended that Create Droplet be checked on enable Backups (Allow automatic backup), although this will be more than 20% of the package fee, the data is priceless!

Installing node. JS under the Debian system

Run the following command in turn

apt-get install curl  
curl -sL https://deb.nodesource.com/setup | bash -  
apt-get install -y nodejs  
apt-get install -y build-essential  

via:https://github.com/joyent/node/wiki/installing-node.js-via-package-manager# Debian-and-ubuntu-based-linux-distributions

Comparison of resource occupancy in each system

Debian, CentOS takes up less memory, Ubuntu occupies a large memory

Tested, Ubuntu frequently receives an alert of 90% in memory when running 4 node. JS sites with low provisioning Digitalocean

Finally, the Debian system was selected

How to reduce the memory footprint of a node. js site Reduce the number of sites

For example, I ran 4 sites, 2 for the public, 2 private, 2 private projects by function divided into two sites, seemingly the organization is very reasonable, but for performance reasons, the 2 together as one after the resource consumption decreased by about One-fourth

Optimize the memory footprint of MySQL on small memory VPS

MySQL default configuration will take up dozens of M of memory, sometimes often due to low memory hangs, need to optimize it

1. Use SQLite instead of MySQL

The disadvantage of SQLite is that concurrent write operations are not supported, and the logical impact on code is large

2. Modify the MySQL configuration

In fact, the MySQL installation comes with several configuration examples, located in/usr/share/doc/mysql-server-5.5/examples/

If the file is a gz suffix, you need to unzip it before you can see the source file, then you get

my-small.cnf  my-medium.cnf  ...

Files, you can view them at the beginning of the file, such asmy-small.cnf

# This is for a system with little memory (<= 64M) where MySQL is only used# from time to time and it‘s important that the mysqld daemon# doesn‘t use much resources.

You can choose the appropriate file replacement /etc/mysql/my.cnf , and then restart MySQL to

/etc/init.d/mysql restart

At present, my own small project uses, on the my-medium.cnf 512M VPS occupies less than 10M of memory, enough to save it

Create a Swap partition

Similar to the virtual memory under Windows, when the memory is low, a portion of the hard disk space virtual into memory to use, so as to solve the memory shortage situation

In particular, when running the MySQL database, in small memory VPS often encountered because of low-memory database is disconnected, then you can create a swap solution

Normally, the VPS is automatically created when the system is installed, and Digitalocean requires us to manually create

Execute the following command sequentially

cd /var  touch swap.img  chmod 600 swap.img  dd if=/dev/zero of=/var/swap.img bs=1024k count=1000  mkswap /var/swap.img  swapon /var/swap.img  echo "/var/swap.img    none    swap    sw    0    0" >> /etc/fstab  echo "vm.swappiness=30" >> /etc/sysctl.conf  echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf  echo "Swap created and added to /etc/fstab for boot up."  

Via Https://www.digitalocean.com/community/tutorials/how-to-configure-virtual-memory-swap-file-on-a-vps
Https://laracasts.com/discuss/channels/forge/does-forge-run-ok-on-a-digitalocean-512mb-instance

Restart the website at timed intervals

Restarting is an effective way to release software resources: P

node. JS Process Daemon

Running node. JS site requires a process daemon, such as PM2, to consider project exception exits, server restarts, etc.

Installing PM2
npm install pm2 -g  
Create a PM2 self-starter script
pm2 startup debian  

This allows the PM2 to run automatically after the VPS system restarts, assuming that the node. js site is PM2 to start

To start a node. js site using PM2

Enter the site root and the original Startup command is

node app.js  

Now use

pm2 start app.js --name myappname  

myappnameInstead of the name you want, the --name reason for the argument is that if you have multiple node. JS sites and their respective startup files are App.js, it is difficult to identify the sites in the process

There are many more uses, such as starting Ghost in production mode

NODE_ENV=production pm2 start index.js --name ghost  

Then save

pm2 save  

Add all the sites in the same way so that PM2 starts all the sites automatically.

View all PM2 processes
pm2 list  

This allows you to see the status of all sites, such as memory usage, restart times, and so on.

┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬──────────────┬──────────┐│ App name │ id │ mode │ PID  │ status │ restarted │ uptime │       memory │ watching │├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼──────────────┼──────────┤│ app      │ 1  │ fork │ 5853 │ online │        99 │ 6h     │ 103.625 MB   │ disabled ││ rss      │ 3  │ fork │ 5857 │ online │        26 │ 6h     │  95.340 MB   │ disabled ││ ghost    │ 4  │ fork │ 5861 │ online │        23 │ 6h     │  92.555 MB   │ disabled │└──────────┴────┴──────┴──────┴────────┴───────────┴────────┴──────────────┴──────────┘
Timed Restart PM2

The system built-in timer functions to perform tasks, edit etc/crontab files, add at the end of the file

00 11 * * * root /usr/bin/pm2 restart all

Note: Be sure to add a blank line at the end, otherwise the last line setting may not execute

Then restart corn for the settings to take effect

/etc/init.d/cron restart

This will set up the automatic restart PM2 every day three o'clock in the morning, to achieve the purpose of restarting all sites

Use every minute of execution to test whether the command is performing properly

*/1 * * * * root /usr/bin/pm2 restart all
Introduction to TIME Format

00 11 * * *This time format is described in Http://zh.wikipedia.org/wiki/Cron#.E6.97.B6.E9.97.B4.E8.AE.BE.E7.BD.AE

You may ask, three o'clock in the morning is not supposed to write? 00 3 * * * Why is it written as 11?

Our server Digitalocean is located in the U.S. San Francisco node, and the input command

date  

You can see that the output is in UTC time, see http://zh.wikipedia.org/wiki/%E5%8D%8F%E8%B0%83%E4%B8%96%E7%95%8C%E6%97%B6

Thu Jan  1 10:42:06 UTC 2015  

Our Beijing time zone is utc+8, while the US time zone is located in UTC-8 ~ UTC-5, when UTC 0 o'clock, Beijing is 0 + 8 = 8, and the UTC-8 time zone is-(24-8) = 16 (16 points the day before)

UTC-8 is also known as Pacific Time, if you are an Apple developer, Apple sent you the holiday mail is the use of the Pacific time, you can not digest the words just remember we are 16 hours faster than Apple is enough

My site is mainly for the United States users, set here is UTC-8 three o'clock in the morning, so is 11, consider UTC-5 is six o'clock in the morning, meet the demand, if you are facing domestic users of three o'clock in the morning, then is 19

Some experience with VPS running node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.