Skip to content

Instantly share code, notes, and snippets.

@jack2jm
Created July 23, 2024 08:07
Show Gist options
  • Save jack2jm/cc5397225252d4e8c3dd5662787bc8c0 to your computer and use it in GitHub Desktop.
Save jack2jm/cc5397225252d4e8c3dd5662787bc8c0 to your computer and use it in GitHub Desktop.
Link - https://chatgpt.com/share/986db3e4-ec79-473e-a749-ee9e359b1f8f
-------
How to set up and run your Node.js application on an Ubuntu server using Apache and NVM, follow these steps:
1. Install NVM (Node Version Manager)
1.1 Install bash:
-------
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
1.2 Source NVM:
----------
source ~/.bashrc
1.3 Verify Installation:
-----------
nvm --version
2. Install Node.js
--------
2.1 Install the desired Node.js version:
nvm install node # or nvm install <version>
2.2 Set the default Node.js version:
nvm alias default node
3. Clone Your Node.js Repository
Clone the repository to your server:
git clone https://github.com/your-repository-url.git
cd your-repository-directory
Install dependencies:
npm install
4. Configure Apache
Install Apache and libapache2-mod-proxy-http:
sudo apt update
sudo apt install apache2 libapache2-mod-proxy-html
Enable required Apache modules:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
5. Create a virtual host configuration:
5.1 Create a new file for your site configuration:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
5.2 Add the following configuration:
<VirtualHost *:80>
ServerName yourdomain.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
5.3 Enable the site and restart Apache:
sudo a2ensite yourdomain.com.conf
sudo systemctl restart apache2
5. Run Your Node.js Application
npm start # or node app.js
Additional Tips
Ensure that your application is listening on localhost or 127.0.0.1 and the correct port (e.g., 3000).
Consider using a process manager like pm2 to keep your application running:
npm install pm2 -g
pm2 start app.js
pm2 startup
pm2 save
This setup should help you get your Node.js application running on Apache with NVM on your Ubuntu server. Let me know if you need any further assistance!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment