How does centos7 set the exchange file? How does centos7 set the exchange file


Source of problem

If you want to build a blog service on the VPS you buy, you prefer dynamic blogs and so on. You choose nodejs, and the scope will be reduced as soon as you see it. At the beginning, you saw n-blog. You need to install bower. If you are installing, you will always be prompted about the NPM version:

npm WARN deprecated This version of npm lacks support for important features,
npm WARN deprecated such as scoped packages, offered by the primary npm
npm WARN deprecated registry. Consider upgrading to at least [email protected], if not the
npm WARN deprecated latest stable version. To upgrade to [email protected], run:
npm WARN deprecated 
npm WARN deprecated   npm -g install [email protected]
npm WARN deprecated 
npm WARN deprecated To upgrade to the latest stable version, run:
npm WARN deprecated 
npm WARN deprecated   npm -g install [email protected]
npm WARN deprecated 
npm WARN deprecated (Depending on how Node.js was installed on your system, you
npm WARN deprecated may need to prefix the preceding commands with `sudo`, or if
npm WARN deprecated on Windows, run them from an Administrator prompt.)
npm WARN deprecated 
npm WARN deprecated If you're running the version of npm bundled with
npm WARN deprecated Node.js 0.10 LTS, be aware that the next version of 0.10 LTS
npm WARN deprecated will be bundled with a version of [email protected], which has some small
npm WARN deprecated backwards-incompatible changes made to `npm run-script` and
npm WARN deprecated semver behavior.

It is clear that the NPM version is too low and some features are not supported. Reference tips, executing

npm -g install [email protected]

After all, NPM has not been upgraded at this time. After waiting for dozens of seconds, it appears as follows

... omitted above
npm WARN deprecated backwards-incompatible changes made to `npm run-script` and
npm WARN deprecated semver behavior.

If you are not careful, you will find that it is inexplicably over, and NPM has not been updated. If you look closely, you will find a killed prompt in the last line. A search on the Internet shows that this is because there is not enough memory in the server, so the program ends directly. In fact, if the VPS memory is small, problems will occur not only when installing the nodejs library, but also when using the yum installation package directly, the error of cannot allocate memory will be prompted.


Upgrade VPS configuration

The easiest way is to upgrade the VPS configuration. Just click the button on the webpage and pay for it. For individual players, it’s just to use VPS to build personal websites and toss about gadgets, which doesn’t have much traffic and often doesn’t need a good configuration. Therefore, the following method is adopted to increase the exchange files.


We usually hear more about the swap partition, that is, we can directly separate a partition on the hard disk to use as the swap space. If it is inconvenient to create a new partition after the system has been installed, you can use files instead. The essence of switching space is hard disk, which is used as a large cache of memory in the system. When the operating system memory is not enough, it will replace some pages that are not commonly used into the switching space. So swap space can improve the performance of some systems. However, the larger the swap space is, the better. If the application just uses the data in the swap space, there will be a large delay at this time. Too large a swap partition will lead to a large number of applications with high delay. Therefore, if the program has large memory requirements, the best way is to increase physical memory. Buying memory is the king. Swap space is just a way to improve performance.

Centos7 settings exchange file

Check system conditions

First, check whether the system has set up exchange space. In principle, only one exchange space should be set as far as possible. In addition, check whether the system has enough free hard disk space.

swapon -s

You can view the swap space of the system through the swapon command, or you can view it through the free command.

free -h

Check the remaining hard disk space of the system through DF command.

df -h

Create a swap file

Create a new exchange file by using the fallocate command. Since my system memory is 512M and the exchange partition is generally set to 2-4 times of memory, I set it to 1g and directly mount it to the root directory.

sudo fallocate -l 1G /swapfile

You can view it through the LS command

ls -lh /swapfile

give the result as follows

-rw-r--r-- 1 root root 1.0G Oct 30 11:00 /swapfile

Enable exchange file

First, modify the / swapfile permission to allow root access only.

sudo chmod 600 /swapfile

The result of LS is as follows

-rw------- 1 root root 1.0G Oct 30 11:00 /swapfile

Next, inform the system to create the swap space

sudo mkswap /swapfile

Finally, enable swap space

sudo swapon /swapfile

The results can be viewed through the swapon-s or df-h commands, and the swap space should be displayed.

Persistent configuration

After reboot, the system will no longer set / swapfile as swap space. You can add the following in / etc / fstab

/swapfile   swap    swap    sw  0   0

In this way, the system will automatically map / swapfile to swap space after reboot.


It can be imagined that if the actual memory demand is far larger than the physical memory size, there will be frequent data exchange between memory and exchange space, and the damage to the hard disk is relatively large. Moreover, the application experience will be relatively poor.

So, swap space is just a temporary solution, and buying memory is the king.