如何在64位的CentOS6.4上安装及运行NodeJS应用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在64位的CentOS6.4上安装及运行NodeJS应用相关的知识,希望对你有一定的参考价值。

1、如果对nodejs环境有比较高的要求,建议选择源码安装的方式进行安装。
你可以到nodejs org官网上面找到相对的tar.gz文件包。
通过wget命令下载到centos服务器上, 然后进行源码安装。
2、如果对版本要求不高,可以直接用centos的包管理器yum进行安装
yum install nodejs npm
3、nodejs版本也可以通过nvm等工具去控制,期待深入研究。
参考技术A Introduction
This article outlines the steps necessary to run a “Hello world” in node.js + express, running on a 64bit Centos 6.4 installation. We will be building the latest version of source (at this moment, v0.10.4) from the upstream provider.
As is written on their homepage, Node.js is a platform built on Chrome’s javascript runtime for easily building fast, scalable network applications. This is a fast, event driven platform and server-side Javascript engine used for building web applications. DigitalOceans’ droplets are a cost-effective way to install and start studying server-side Javascript and bulding or deploying web applications with Node.js.
Setup a VPS
To get started, we will need a droplet – the smallest instance will do just enough – and a SSH client (ie. Putty on Windows, Linux systems and Mac Os X usually have it out of the box). When we receive our initial root password, we can ssh into the instance. SSH into the VPS and change the root password, if you haven’t already. It would probably be a good idea to also update software repository to the latest versions:
yum -y update
This will update installed software on our VPS to the latest versions.
Yum can take a few minutes, but when it’s done, we need to prepare for software installation. We’re going to build Node.js from the latest source available at the moment of writing this (v0.10.4). To do that, we’ll need “Development Tools”. It’s a group of tools for compiling software from sources.
yum -y groupinstall "Development Tools"
This command will pull a “Development Tools” group with the applications needed to compile node.js.
Also, we’ll install GNU screen – a piece of software that allows us to connect to our VPS, start a session and detach from it. We could disconnect and connect later, or from another workstation, and pick up where we left. It’s very handy, especially during development of an app, when we want to learn stuff.
yum -y install screen
Node.js installation
Now that we’re ready to install Node.js from sources. First, we’ll move to /usr/src directory – the usual place to hold software sources.
cd /usr/src
Now, we pick the latest compressed source archive from Node.js website at http://nodejs.org/download/.
wget http://nodejs.org/dist/v0.10.4/node-v0.10.4.tar.gz
We could and should replace the url and use the more recent version of node.js, if there is one. Next, we are uncompressing the source files and move into that directory.
tar zxf node-v0.10.4.tar.gz
cd node-v0.10.4
Now the source for Node.js is extracted and we’re in the source directory. We can now prepare our compiler commands, by executing the configure script:
./configure
It will read the properties of our system to prepare compiler flags. Ie. it could be a system architecture (32/64bit, CPU specific flags etc). With it, we’re ready to actually compile the source now. To do that, just type:
make
This is probably the most time-consuming task here: on my example VPS it took about 6 minutes and 34 seconds to complete. When we’re done, we need to make this available system-wide:
make install
The latest command will place the compiled binaries in system path, so all users could use it without any further setup. By default, node binary should be installed in /usr/local/bin/node.
Install Express.js
We now have Node.js installed and complete, we can start developing right away, deploy an already done application or we can proceed to create our Express.js application. First, we’ll use npm, nodes’ module manager, to install express middleware and supervisor – a helpful module that keeps our app started, monitors for file changes (ie. when we’re developing the app) and restarts the VPS when needed.
UPDATE: To be able to run an executable in /usr/local/bin through sudo, you have add /usr/local/bin to your secure_path using visudo.
sudo visudo
Look for secure_path, and append the following to it: “:/usr/local/bin”. Once you have done that, you’re now ready to install the express and supervisor modules.
npm -g install express express-generator supervisor
npm -g install will install the express and supervisor modules from npm software repository and make it available to the whole system. The -g switch in this command means “global” – the express and supervisor commands will be available accross the whole system.
Add non-privileged user
You should now, for security reasons, create a regular system user and run node under non-privileged account.
To do this, add the user first. You can replace “exampleuser” with whatever name your prefer.
useradd exampleuser
We have a new system user. Add a decent password for the new user:
passwd exampleuser
Log out, and log back in as the new user.
This changes our login shell from root (system user) to exampleuser (non-privileged user who can compromise the system with less damage).
Creating an express app
Express is powerfull framework, and to create our first application, all we have to do is type:
express hello
The command will create a “hello” directory and setup some basics for a new application. Now we should enter this directory and install express dependencies:
cd hello && npm install
npm install part of the command will read all the module dependencies from a generated package.json file and install it from npm software repository.
We should start a new screen session so we can leave node app running:
screen
Finally, we can start our application with the help of supervisor that we installed earlier.
supervisor ./bin/www
Now we’re able to access our first express app at your VPS IP. For examplehttp://123.456.78.90:3000/.本回答被提问者采纳

Centos7上安装及配置Apache

Apache HTTP服务器是世界上最流行的Web服务器。 它是一款免费的开源和跨平台的HTTP服务器,提供强大的功能,可以通过各种模块进行扩展。 以下说明介绍如何在CentOS 7机器上安装和管理Apache Web服务器。

安装Apache

Apache在默认的CentOS仓库中可用,安装非常简单。 在CentOS和RHEL上,Apache软件包和服务称为httpd。 要安装软件包,请运行以下命令:

sudo yum install httpd

安装完成后,启用并启动Apache服务:

sudo systemctl enable httpd
sudo systemctl start httpd

如果您正在运行防火墙,则还需要打开HTTP和HTTPS端口80和443:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

我们可以通过以下方式检查Apache服务的状态和版本:

sudo systemctl status httpd

输出:

● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Active: active (running) since Thu 2018-04-26 07:13:07 UTC; 11s ago
    Docs: man:httpd(8)
          man:apachectl(8)
 Main PID: 3049 (httpd)
  Status: "Total requests: 0; Current requests/sec: 0; Current traffic:  0 B/sec"
  CGroup: /system.slice/httpd.service
          ├─3049 /usr/sbin/httpd -DFOREGROUND
          ├─3050 /usr/sbin/httpd -DFOREGROUND
          ├─3051 /usr/sbin/httpd -DFOREGROUND
          ├─3052 /usr/sbin/httpd -DFOREGROUND
          ├─3053 /usr/sbin/httpd -DFOREGROUND
          └─3054 /usr/sbin/httpd -DFOREGROUND

再输入如下命令:

sudo httpd -v

输出:

Server version: Apache/2.4.6 (CentOS)
Server built:  Oct 19 2017 20:39:16

最后,要验证安装,在您所选择的浏览器中打开您的服务器IP地址http://YOUR_IP,您将看到默认的Apache欢迎页面,如下所示:

 

使用systemctl管理Apache服务

我们可以像任何其他系统单元一样管理Apache服务。

要停止Apache服务,请运行:

sudo systemctl stop httpd

要再次启动,请键入:

sudo systemctl start httpd

重新启动Apache服务:

$sudo systemctl restart httpd

在进行一些配置更改后重新加载Apache服务:

$sudo systemctl reload httpd

如果您想禁用Apache服务以在启动时启动:

$sudo systemctl disable httpd

并重新启用它:

$sudo systemctl enable httpd

以上是关于如何在64位的CentOS6.4上安装及运行NodeJS应用的主要内容,如果未能解决你的问题,请参考以下文章

Mongodb安装(Centos 6.4 32位)

win8.1 64位的笔记本,打开cuda 6 notebook 安装文件时显示此应用无法在你的电脑上运行

如何在64位windows7上同时使用32位和64位的Eclipse

配置:使安装在64位系统上的IIS7支持32位的应用程序

Apache的下载安装(主要说的 64位)及问题

CentOS6.4安装Zookeeper-3.4.12图解教程