Linux vỡ lòng, vừa làm vừa note lại

Xem các module đã enabled trong Apache

httpd -M

Kiểm tra phiên bản Apache hiện tại

/usr/sbin/httpd -v

Khi gặp lỗi > error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory

Thì chạy 2 lệnh sau để fix lỗi

ldconfig
service httpd start

Xem hệ điều hành linux 32bit hay 64bit

uname -m

Nếu thấy kết quả x86_64 thì có nghĩa là x64Tải nodejs về

curl --silent --location https://rpm.nodesource.com/setup_9.x | sudo bash -

Sau đó cài đặt

sudo yum -y install nodejs

Để biên dịch (compile) và cài đặt các native addons từ npm chúng ta cần cài đặt build tools

sudo yum install gcc-c++ make

Xem phiên bản nodejsnpm đã cài đặt

node -v
npm -v

Kiểm tra cài đặt nodejs bằng cách tạo file

nano hello_world.js

Sau đó dán nội dung này vào và lưu file

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello NodeJs + Apache!\n');
}).listen(8080, '0.0.0.0');

Chạy test ứng dụng nodejs

node hello_world.js

Để chạy ứng dụng nodejs chúng ta cần có pm2 (Production Process Manager for Node.js applications with a built-in Load Balancer), cài đặt pm2

sudo npm install pm2@latest -g

Chạy ứng dụng nodejs

pm2 start hello_world.js

Cách dùng mod_proxy của Apache để truy cập ứng dụng nodejs qua Apache webserver, bằng cách thêm 3 dòng sau vào trong file cấu hình host của Apache, có thể thực hiện bằng DirectAdmin

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Enable socket.io

npm install --save express@4.15.2
npm install --save socket.io

Sau đó thay nội dung trong bằng

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://localhost:8080%{REQUEST_URI} [P,L]

ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ProxyPass /socket.io http://localhost:8080/socket.io/
ProxyPassReverse /socket.io http://localhost:8080/socket.io/
/etc/rc.d/init.d/httpd restart

Sau đó viết ứng dụng nodejs dùng socket.io theo hướng dẫn demo tại https://socket.io/get-started/chat/Xem thử kết quả: https://vietmeta.com

Mr.Phan

KTS, KSXD, Developer

You may also like...

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *