Ensure you have these installed:
- PostgreSQL (https://www.postgresql.org/download/)
- NodeJS (https://nodejs.org/en/download/)
- Redis (Google how to install this, may differ across platforms)
- Visual Studio Code (Optional)
For first-time installation/beginners, follow these steps after the installation (non-first-timers should know what to do). Note this is only for development environment.
-
Change CLI user to postgres (Make sure you start the PostgreSQL service before this - Google it)
sudo su postgres -
Login into postgres via peer authentication through postgres user
psql -
Change password to 'postgres'
\password -
Exit psql
\q -
Create a database using postgres user, named 'classify'
createdb classify -
Exit postgres user
exit -
Then restart PostgreSQL service
Install yarn globally for easy usage
npm install -g yarn
git clone https://github.com/ngjunkang/classify.git
# Go to respective folder
cd classify/frontend
cd classify/backend
# To download required node modules
yarn
NodeJs allows us to configure alias to run commands, under scripts in package.json, and we can run it with 'yarn SCRIPT_NAME'.
# To compile and run the NextJS application, and watch for changes (run this during development)
yarn dev
# To compile the NextJS application into JavaScript files in .next directory
yarn build
# To run the JavaScript files in .next directory
yarn start
# To generate the types for each variables in the GraphQL schemas
yarn gen
# To compile TypeScript files in src into JavaScript files in dist and watch for changes in the TypeScript files (run this during development)
yarn watch
# To run the compiled Javascript files in dist and watch for changes (run this during development)
yarn dev
# To compile TypeScript files in src into JavaScript files in dist
yarn build
# To run the compiled Javascript files in dist
yarn start
# To generate the types of the environment variables configured in .env into src/env.d.ts as well as the .env.example file for environment variables checking
yarn gen-env
Create a file called .env.local in the frontend directory and it should contain:
-
NEXT_PUBLIC_API_URL=backendAPIAddress
NEXT_PUBLIC_API_URL=http://localhost:4000/graphql -
NEXT_PUBLIC_SUBSCRIPTION_WS=backendAPISubscriptionAddress
NEXT_PUBLIC_SUBSCRIPTION_WS=ws://localhost:4000/subscriptions
*Note: Any variables must be preceded by NEXT_PUBLIC_
- Sign up for Vercel.
- Link your Git account to Vercel.
- Choose the repository you want to deploy and choose NextJS as the Framework Preset and change the directory to deploy to frontend.
- Wait for it to deploy.
- Go to project settings.
- Add a environment variable called NEXT_PUBLIC_API_URL with a value of https://api.classify.page/graphql
- Also go to Domain settings and follow the instructions to link to your domain name provider.
This setup is for ease of switching between development and production.
Copy the template from .env.example to a new file .env in the backend directory. The format of the variables is as follows:
-
DATABASE_URL=databaseType://username:password@host:port/databaseName
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/classify -
REDIS_URL=host:port
REDIS_URL=localhost:6379 (Set this as default) -
PORT=port
PORT=4000 -
SESSION_SECRET=randomString
SESSION_SECRET=hkjasdnmfdeeadsldxfdhderaqwqaq -
CORS_ORIGIN=clientAddress
CORS_ORIGIN=http://localhost:3000 -
EMAIL=gmailToSendEmailFrom
-
EMAIL_PASSWORD=gmailPassword
Extra environment variables for production only:
- CDOMAIN=domainName E.g. CDOMAIN=.classify.page
- NODE_ENV=production
Note: Run this when you add new environment variables:
npx gen-env-types .env -o src/env.d.ts -e .
OR
yarn gen-env
This basic guide is only applicable for Linux server, specifically Ubuntu/Debian. Not applicable to large servers in enterprise.
-
Update the dependency repository information (or the software versions that the system requires)
sudo apt-get update -
Install Node.js v16.x from NodeSource Binary Distributions (comes with npm)
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt-get install -y nodejs -
Install Nginx
sudo apt-get install nginx -
Install UFW (a simpler IP table)
sudo apt-get install ufw -
Install Redis
sudo apt-get install redis-server -
Install PostgreSQL
# Create the file repository configuration: sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' # Import the repository signing key: wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - # Update the package lists: sudo apt-get update # Install the latest version of PostgreSQL. sudo apt-get -y install postgresql -
Install Git
sudo apt-get install git -
Install Certbot for installing HTTPS Certificate
# Add Certbot’s repository sudo add-apt-repository ppa:certbot/certbot # Update your package lists sudo apt-get update # Install Certbot and additional required packages: sudo apt-get install certbot python-certbot-nginx
-
Set up PostgreSQL and NodeJS (use sudo) as above.
-
Set up UFW
# Ensure startup on boot sudo ufw enable # Allow routing on TCP Port 80 (HTTP) and 443 (HTTPS) sudo ufw allow 80/tcp sudo ufw allow 443/tcp # Reload firewall sudo ufw reload # Ensure that the routing table is correct sudo ufw status -
Start Redis and ensure that it's working
# Start Redis server sudo systemctl start redis # Run Redis CLI redis-cli # Test ping returns PONG -
Clone project to server
git clone https://github.com/ngjunkang/classify.git -
Create the environment file, .env as stated above
-
Go to backend and install the required node modules
# Change directory cd classify/backend # Install the required node modules yarn -
Compile the backend code
yarn build -
Set up system unit file to run backend as a service
# Open a editor for the service file of classify sudo nano /etc/systemd/system/classify.service # Contents of /etc/systemd/system/classify.service (Note: replace <...> with your ...) [Unit] Description=Classify backend express API After=network.target Wants=redis.service [Service] User=<YOUR USERNAME> Group=www-data WorkingDirectory=<ABSOLUTE DIRECTORY OF PROJECT>/classify/backend Environment=CDOMAIN=.classify.page Environment=NODE_ENV=production EnvironmentFile=<ABSOLUTE DIRECTORY OF PROJECT>/classify/backend/.env ExecStart=/usr/bin/node <ABSOLUTE DIRECTORY OF PROJECT>/classify/backend/dist/index.js Restart=always RestartSec=3 [Install] WantedBy=multi-user.target # End of file # Reload daemon sudo systemctl daemon-reload # Enable start-on-boot sudo systemctl enable classify # Start Classify service sudo systemctl start classify # To check the service logs sudo journalctl -u classify -
Set up Nginx server file to run a reverse proxy in front of the Node application
# Open a editor for the server file of classify sudo nano /etc/nginx/sites-available/classify # Contents of /etc/nginx/sites-available/classify (Note: This is assuming that Classify runs on port 1234, feel free to change it!) server { listen 80; server_name api.classify.page; location / { proxy_pass http://localhost:1234; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; } } # End of file # Check the syntax of the file sudo nginx -t # Enable the site by creating symbolink sudo ln -s /etc/nginx/sites-available/classify /etc/nginx/sites-enabled # Start/restart Nginx service sudo systemctl restart nginx -
Port forward the IP address of your server out to the Wide Area Network (WAN) using your router (http://192.168.1.254). Example:
LAN Port Range WAN Port Range TCP/UDP IP Address of server 443 ~ 443 443 ~ 443 TCP only 192.168.1.23 80 ~ 80 80 ~ 80 TCP only 192.168.1.23 *Note: Also recommended to set a static DHCP for your server.
-
Install HTTPS certificates & convert the server file to HTTPS (Follow the CLI wizard)
sudo certbot --nginx