Please note that current aapanel method of deploying node js using website creation mode is not suitable for production. Because it can go offline when the load increases and exhaust your resources. So best way for production is to use pm2 or systemd manually.I decided to use systemd, as its more light weight and i don't need cluster mode. So i used systemd (Which is a linux native process manager) for maanging automatic restart in case crashes, and starting after a reboot. (With systemd you cannot have cluster mode dpeloyment for node js. ) if you need cluster mode, you have to use pm2 just like how you configure systemd.
I can only explain how we deploy node js, react and Next js app.
- Upload the source code to www/wwwroot/myapp
- Build using aapanel node js creation tool
- Please note that you should add your run parameters like start, build, etc in package.json.
- Use first build command in the RUn options window to build your project
- Now, you code is build. and is in the directory www/www/root/yourappfolder.
- setup a systemd service file and enable it using sytemctl enable yoursystemdfilename.service
- then start the service by systemctl start yoursystemdfile.service
Now the app will be working in your localhost with port mentioned in the service file.
Sample service file for next js app
[Unit]
Description=BiopageAppService
[Service]
WorkingDirectory=/www/wwwroot/biopageapp
ExecStart=/www/server/nodejs/v20.15.1/bin/node /www/wwwroot/yourappdirectory/node_modules/.bin/next start -p 3001
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=yourapp-name
User=www
Environment=NODE_ENV=production PORT=3001
[Install]
WantedBy=multi-user.target
If its a express server. just upload and use this command
Just upload in the directory www/wwwroot/
and enable the systemd service and then start it
[Unit]
Description=Service description
[Service]
WorkingDirectory=/www/wwwroot/yourappfolder
ExecStart=/www/server/nodejs/v20.15.1/bin/node /www/wwwroot/your appfolderpath/index.js
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=clapup-auth
User=www
Environment=NODE_ENV=production PORT=3600
[Install]
WantedBy=multi-user.target
Once you deployed in localhost with port. You can setup a reverse proxy with php static website.
To configure Nginx as a reverse proxy for our website, go to Websites and under the configuration of our website, we have a Config section. This is the primary configuration file of the site and here we will be able to add some rules for reverse proxy. First, remove the rules for images and js|css, and then add the following rule:
location / {
proxy_pass http://0.0.0.0:portnumber;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
If you use websockets, use appropriate nginx header to have connection upgrade.