Full-Stack Deployment: Angular + Express on CentOS

Full-Stack Deployment: Angular + Express on CentOS

Dec 13, 2017 · 3 min read · 462 Words · -Views -Comments

I recently needed to deploy a web application on a cloud server. Here are my documented notes for anyone with similar requirements.

Deployment Environment

  • Server OS: CentOS release 6.8 (Final)
  • Network: Connected to Internet
  • Development Machine: Windows (or macOS)

1. Terminal Connection

Use a terminal emulator like PuTTY (Windows) or iTerm2 (macOS) to establish an SSH connection to your target server.

2. Setting Up Node.js with NVM

While you can install Node.js directly, using NVM (Node Version Manager) is highly recommended for easy version management.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

Note: After installation, you may need to restart your terminal session for the nvm command to be recognized.

# Install the Node.js LTS version (e.g., version 6 in this example)
nvm install 6

Optimizing NPM with NRM

NRM helps manage npm registries. Switching to a local mirror (like Taobao for developers in China) can significantly speed up package downloads.

npm i nrm -g
nrm use taobao

3. Preparing the Project Directory

According to the Linux Filesystem Hierarchy Standard, /var is a good place for variable data like web applications.

mkdir /var/www/my-project

Uploading Files

Upload your Frontend dist folder, Backend source code, and optionally node_modules (though running npm install on the server is usually cleaner) using tools like WinSCP (Windows) or Cyberduck/SCP (macOS).

4. Process Management with PM2

PM2 is a production-grade process manager for Node.js applications. It ensures your app stays alive and restarts if it crashes.

npm i pm2 -g

Configuring Environment Variables

To distinguish between development and production configurations in your Express app:

vi /etc/profile
# Add the following line at the end:
export NODE_ENV="production"

# Apply the changes immediately:
source /etc/profile

Starting the Application

cd /var/www/my-project
pm2 start app.js --name='my-app'

# Ensure the app restarts if the server reboots:
pm2 save
pm2 startup

5. Web Server Setup: Nginx

Nginx will act as a reverse proxy, sitting in front of our Node.js application.

yum -y install nginx

Reverse Proxy Configuration

Edit the configuration file at /etc/nginx/conf.d/default.conf. We want Nginx to listen on port 80 and forward traffic to our app (running on port 3001 in this example).

server {
    listen       80;
    server_name  example.com; # Replace with your domain

    location / {
         proxy_pass http://localhost:3001;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
    }
}

Managing Nginx

service nginx start    # Start service
chkconfig nginx on     # Enable auto-start on boot
nginx -s reload        # Reload config after changes

6. Firewall Configuration

Ensure port 80 is open to external traffic:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save

Final Thoughts

Deployment Success

Deployment complete! You can now access your application via the server’s IP address or domain name. While this guide was written based on a Windows environment, the server-side steps are identical for macOS users.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover