├── Deploy_MERN_on_VPS.md ├── MariaDB_Setup_on_VPS.md ├── MongoDB_Setup_on_VPS.md ├── MySQL_Setup_on_VPS.md ├── PostgreSQL_Setup_on_Vps.md └── Vercel_JSON_Config_For_MERN.md /Deploy_MERN_on_VPS.md: -------------------------------------------------------------------------------- 1 | 2 | ## Deploying MERN Stack Project on Hostinger VPS 3 | 4 | - Preparing the VPS Environment 5 | - Setting Up the MongoDB Database 6 | - Deploying the Express and Node.js Backend 7 | - Deploying the React Frontends 8 | - Configuring Nginx as a Reverse Proxy 9 | - Setting Up SSL Certificates 10 | 11 | ### 1. Preparing the VPS Environment 12 | 13 | #### Get you VPS Hosting here (with 20% discount) : 14 | - [Hostinger VPS KVM1](https://www.hostinger.com/cart?product=vps%3Avps_kvm_1&period=12&referral_type=cart_link&REFERRALCODE=FULEBEHEMFY4&referral_id=01958194-af72-7176-905a-70836d0d6ab7) 15 | - [Hostinger VPS KVM2](https://www.hostinger.com/cart?product=vps%3Avps_kvm_2&period=12&referral_type=cart_link&REFERRALCODE=FULEBEHEMFY4&referral_id=01958196-63c2-703b-9ce3-2149c8ba44c6) 16 | - [Hostinger VPS KVM4](https://www.hostinger.com/cart?product=vps%3Avps_kvm_4&period=12&referral_type=cart_link&REFERRALCODE=FULEBEHEMFY4&referral_id=01958196-3bcc-717b-89a0-a86f7f605700) 17 | - [Hostinger VPS KVM8](https://www.hostinger.com/cart?product=vps%3Avps_kvm_8&period=12&referral_type=cart_link&REFERRALCODE=FULEBEHEMFY4&referral_id=01958196-12eb-71dc-9f2a-3f506c4ebc7c) 18 | 19 | Log in to Your VPS in Terminal 20 | ```bash 21 | ssh root@your_vps_ip 22 | ``` 23 | 24 | Update and Upgrade Your System 25 | ```bash 26 | sudo apt update 27 | ``` 28 | ```bash 29 | sudo apt upgrade -y 30 | ``` 31 | 32 | Install Node.js and npm (if not pre-installed) 33 | ```bash 34 | curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash - 35 | ``` 36 | ```bash 37 | sudo apt-get install -y nodejs 38 | ``` 39 | 40 | Install Git 41 | ```bash 42 | sudo apt install -y git 43 | ``` 44 | 45 | ### 2. Setting Up the MongoDB Database 46 | 47 | If you want to setup MongoDB on VPS Follow this Guide: [click here](https://github.com/manuelebeh/deployment_notes/blob/main/MongoDB_Setup_on_VPS.md) 48 | 49 | ### 3. Deploying the Express and Node.js Backend 50 | 51 | Clone Your Backend Repository 52 | ```bash 53 | mkdir /var/www 54 | ``` 55 | ```bash 56 | cd /var/www 57 | ``` 58 | ```bash 59 | git clone https://github.com/yourusername/your-repo.git 60 | ``` 61 | ```bash 62 | cd your-repo/backend 63 | ``` 64 | 65 | Install Dependencies 66 | ```bash 67 | npm install 68 | ``` 69 | 70 | Create .env file & configure Environment Variables 71 | ```bash 72 | nano .env 73 | ``` 74 | 75 | add environment variables then save and exit (Ctrl + X, then Y and Enter). 76 | 77 | Installing pm2 to Start Backend 78 | ```bash 79 | npm install -g pm2 80 | ``` 81 | ```bash 82 | pm2 start server.js --name project-backend 83 | ``` 84 | 85 | Start Backend on startup 86 | ```bash 87 | pm2 startup 88 | ``` 89 | ```bash 90 | pm2 save 91 | ``` 92 | 93 | Allowing backend port in firewall 94 | ```bash 95 | sudo ufw status 96 | ``` 97 | 98 | If firewall is disable then enable it using 99 | ```bash 100 | sudo ufw enable 101 | ``` 102 | ```bash 103 | sudo ufw allow 'OpenSSH' 104 | ``` 105 | ```bash 106 | sudo ufw allow 4000 107 | ``` 108 | 109 | ### 4. Deploying the React Frontends 110 | 111 | Creating Build of React Applications 112 | ```bash 113 | cd path-to-your-first-react-app 114 | ``` 115 | ```bash 116 | npm install 117 | ``` 118 | If you have ".env" file in your project 119 | 120 | Create .env file and paste the variables 121 | ```bash 122 | nano .env 123 | ``` 124 | Create build of project 125 | ```bash 126 | npm run build 127 | ``` 128 | 129 | Repeat for the second or mulitiple React app. 130 | 131 | Install Nginx 132 | ```bash 133 | sudo apt install -y nginx 134 | ``` 135 | 136 | adding Nginx in firewall 137 | ```bash 138 | sudo ufw status 139 | ``` 140 | ```bash 141 | sudo ufw allow 'Nginx Full' 142 | ``` 143 | 144 | Configure Nginx for React Frontends 145 | ```bash 146 | nano /etc/nginx/sites-available/yourdomain1.com.conf 147 | ``` 148 | ```bash 149 | server { 150 | listen 80; 151 | server_name yourdomain1.com www.yourdomain1.com; 152 | 153 | location / { 154 | root /var/www/your-repo/frontend/dist; 155 | try_files $uri /index.html; 156 | } 157 | } 158 | ``` 159 | Save and exit (Ctrl + X, then Y and Enter). 160 | 161 | Create a similar file for the second or multiple React app. 162 | ```bash 163 | nano /etc/nginx/sites-available/yourdomain2.com.conf 164 | ``` 165 | ```bash 166 | server { 167 | listen 80; 168 | server_name yourdomain2.com www.yourdomain2.com; 169 | 170 | location / { 171 | root /var/www/react-app-2/dist; 172 | try_files $uri /index.html; 173 | } 174 | } 175 | ``` 176 | 177 | Create symbolic links to enable the sites. 178 | ```bash 179 | ln -s /etc/nginx/sites-available/yourdomain1.com.conf /etc/nginx/sites-enabled/ 180 | ``` 181 | ```bash 182 | ln -s /etc/nginx/sites-available/yourdomain2.com.conf /etc/nginx/sites-enabled/ 183 | ``` 184 | 185 | Test the Nginx configuration for syntax errors. 186 | ```bash 187 | nginx -t 188 | ``` 189 | ```bash 190 | systemctl restart nginx 191 | ``` 192 | 193 | ### 5. Configuring Nginx as a Reverse Proxy 194 | 195 | Update Backend Nginx Configuration 196 | 197 | ```bash 198 | nano /etc/nginx/sites-available/api.yourdomain.com.conf 199 | ``` 200 | ```bash 201 | server { 202 | listen 80; 203 | server_name api.yourdomain.com; 204 | 205 | location / { 206 | proxy_pass http://localhost:4000; 207 | proxy_set_header Host $host; 208 | proxy_set_header X-Real-IP $remote_addr; 209 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 210 | proxy_set_header X-Forwarded-Proto $scheme; 211 | } 212 | } 213 | ``` 214 | 215 | Create symbolic links to enable the sites. 216 | ```bash 217 | ln -s /etc/nginx/sites-available/api.yourdomain.com.conf /etc/nginx/sites-enabled/ 218 | ``` 219 | 220 | Restart nginx 221 | ```bash 222 | systemctl restart nginx 223 | ``` 224 | 225 | ### Connect Domain Name with Website 226 | 227 | Point all your domain & sub-domain on VPS IP address by adding DNS records in your domain manager 228 | 229 | Now your website will be live on domain name 230 | 231 | ### 6. Setting Up SSL Certificates 232 | 233 | Install Certbot 234 | ```bash 235 | sudo apt install -y certbot python3-certbot-nginx 236 | ``` 237 | 238 | Obtain SSL Certificates 239 | ```bash 240 | certbot --nginx -d yourdomain1.com -d www.yourdomain1.com -d yourdomain2.com -d api.yourdomain.com 241 | ``` 242 | 243 | Verify Auto-Renewal 244 | ```bash 245 | certbot renew --dry-run 246 | ``` 247 | -------------------------------------------------------------------------------- /MariaDB_Setup_on_VPS.md: -------------------------------------------------------------------------------- 1 | ## Setup MariaDB on VPS 2 | 3 | ### 1. install MariaDB on VPS 4 | 5 | Update packets 6 | ```bash 7 | apt update && apt upgrade -y 8 | ``` 9 | 10 | Install MariaDB 11 | ```bash 12 | apt install mariadb-server mariadb-client 13 | ``` 14 | 15 | Check that MariaDB is installed 16 | ```bash 17 | systemctl status mariadb 18 | ``` 19 | 20 | If MariaDB is not active, start it : 21 | ```bash 22 | systemctl start mariadb 23 | systemctl enable mariadb 24 | ``` 25 | 26 | ### 2. Secure MariaDB (Recommended) 27 | 28 | ```bash 29 | mariadb-secure-installation 30 | ``` 31 | 32 | Answer the questions: 33 | - Enter current password for root: Press Enter (no default password). 34 | - Switch to unix_socket authentication: Y (additional security). 35 | - Change the root password? : Y → Set a strong password. 36 | - Remove anonymous users? : Y. 37 | - Disallow root login remotely : Y (unless you need remote root access). 38 | - Remove test database and access to it? : Y. 39 | - Reload privilege tables now? : Y. 40 | 41 | ### 3. Create a database 42 | 43 | Connect to MariaDB : 44 | ```bash 45 | mariadb -u root -p 46 | ``` 47 | 48 | Once connected, create a new database: 49 | ```bash 50 | CREATE DATABASE test_db; 51 | ``` 52 | 53 | Next, create a MariaDB user and give him/her the permissions : 54 | ```bash 55 | CREATE USER 'user'@'localhost' IDENTIFIED BY 'strongpassword'; 56 | GRANT ALL PRIVILEGES ON test_db.* TO 'user'@'localhost'; 57 | FLUSH PRIVILEGES; 58 | EXIT; 59 | ``` 60 | 61 | MariaDB URI to Connect with projects: 62 | ```bash 63 | mariadb://user_name@localhost:3306/database_name #without password 64 | mariadb://user_name:password@localhost:3306/database_name #with password 65 | ``` 66 | 67 | ### 4. Final check 68 | 69 | Test the connection with the new user: 70 | ```bash 71 | mariadb -u user -p # Enter password 72 | ``` 73 | 74 | Check databases: 75 | ```bash 76 | SHOW DATABASES; 77 | ``` 78 | 79 | ### 5. Some key points : 80 | 81 | #### 1. Root authentication : 82 | 83 | If you enabled Unix socket authentication when installing mariadb-secure, you can log in without a password, provided their system username matches an authorized MariaDB user via: 84 | ```bash 85 | sudo mariadb -u root # without “-p” 86 | ``` 87 | 88 | #### 2. User privileges : 89 | 90 | Avoid `GRANT ALL PRIVILEGES` unless necessary. Give priority to specific rights: 91 | ```bash 92 | GRANT SELECT, INSERT, UPDATE, DELETE ON test_db.* TO 'user'@'localhost'; 93 | ``` 94 | 95 | #### 3. Remote access : 96 | 97 | If the user needs remote access, replace 'localhost' with '%' (and configure the firewall): 98 | ```bash 99 | CREATE USER 'user'@'%' IDENTIFIED BY 'strongpassword'; 100 | ``` 101 | -------------------------------------------------------------------------------- /MongoDB_Setup_on_VPS.md: -------------------------------------------------------------------------------- 1 | ## Setup MongoDB on VPS 2 | 3 | Install MongoDB 4 | ```bash 5 | sudo apt-get install gnupg curl 6 | ``` 7 | ```bash 8 | curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \ 9 | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \ 10 | --dearmor 11 | ``` 12 | 13 | Create the MongoDB list file. 14 | ```bash 15 | echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list 16 | ``` 17 | 18 | Update package lists and install MongoDB. 19 | ```bash 20 | sudo apt-get update 21 | ``` 22 | ```bash 23 | sudo apt-get install -y mongodb-org 24 | ``` 25 | 26 | Start and enable MongoDB service. 27 | ```bash 28 | sudo systemctl daemon-reload 29 | ``` 30 | ```bash 31 | sudo systemctl start mongod 32 | ``` 33 | ```bash 34 | sudo systemctl enable mongod 35 | ``` 36 | 37 | Verify that MongoDB has started successfully. 38 | ```bash 39 | sudo systemctl status mongod 40 | ``` 41 | 42 | MongoDB URI to connect with projects. 43 | ```bash 44 | mongodb://127.0.0.1:27017 45 | ``` 46 | 47 | ### Configure MongoDB Authentication ( Optional ) 48 | 49 | #### Allowing MongoDB through firewall 50 | 51 | Enable firewall 52 | ```bash 53 | sudo ufw enable 54 | ``` 55 | 56 | Check Port is Allowed through Firewall 57 | ```bash 58 | sudo ufw status 59 | ``` 60 | 61 | If Port is not Allowed then Allow it through Firewall 62 | ```bash 63 | sudo ufw allow 27017 64 | ``` 65 | ```bash 66 | sudo ufw allow 'OpenSSH' 67 | ``` 68 | 69 | Restart MongoDB 70 | ```bash 71 | sudo service mongod restart 72 | ``` 73 | 74 | #### Secure MongoDB by setting up Super User. 75 | 76 | Connect to MongoDB shell 77 | ```bash 78 | mongosh 79 | ``` 80 | 81 | Show database 82 | ```bash 83 | show dbs 84 | ``` 85 | 86 | Change to admin database 87 | ```bash 88 | use admin 89 | ``` 90 | 91 | Create superuser with all privileges 92 | ```bash 93 | db.createUser({user: "username-here" , pwd: passwordPrompt() , roles: ["root"]}) 94 | ``` 95 | 96 | Now Exit mongosh shell 97 | ```bash 98 | .exit 99 | ``` 100 | 101 | Enable Authorization removing comment 102 | ```bash 103 | sudo nano /etc/mongod.conf 104 | ``` 105 | ```bash 106 | security: 107 | authorization: enabled 108 | ``` 109 | 110 | Restart MongoDB 111 | ```bash 112 | sudo service mongod restart 113 | ``` 114 | 115 | To Access Mongo Shell as Super User use this command: 116 | ```bash 117 | mongosh --port 27017 --authenticationDatabase "admin" -u "username-here" -p "password-here" 118 | ``` 119 | 120 | Create Database & User for project: 121 | ```bash 122 | use database_name 123 | ``` 124 | ```bash 125 | db.createUser({user:"username_here", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]}) 126 | ``` 127 | 128 | Now Exit mongosh shell 129 | ```bash 130 | .exit 131 | ``` 132 | 133 | MongoDB URI to Connect with projects: 134 | ```bash 135 | mongodb://username-here:password-here@127.0.0.1:27017/database_name 136 | ``` 137 | -------------------------------------------------------------------------------- /MySQL_Setup_on_VPS.md: -------------------------------------------------------------------------------- 1 | ## Setup MySql on VPS 2 | 3 | ### 1. install MySQL on VPS 4 | 5 | Update packets 6 | ```bash 7 | apt update && apt upgrade -y 8 | ``` 9 | 10 | Install MySQL 11 | ```bash 12 | apt install mysql-server -y 13 | ``` 14 | 15 | Verify that MySQL is installed 16 | ```bash 17 | systemctl status mysql 18 | ``` 19 | 20 | If MySQL is not active, start it : 21 | ```bash 22 | systemctl start mysql 23 | systemctl enable mysql 24 | ``` 25 | 26 | ### 2. Secure MySQL (optional but recommended) 27 | 28 | ```bash 29 | mysql_secure_installation 30 | ``` 31 | 32 | Answer the questions: 33 | - Configure VALIDATE PASSWORD plugin (Yes, recommended) 34 | - Change root password (Yes, recommended) 35 | - Delete anonymous users (Yes) 36 | - Disable remote root access (Yes, unless remote access is required) 37 | - Delete test database (Yes) 38 | - Reload privileges (Yes) 39 | 40 | ### 3. Create a database 41 | 42 | Connect to MySQL : 43 | ```bash 44 | mysql -u root -p 45 | ``` 46 | 47 | Once connected, create a new database: 48 | ```bash 49 | CREATE DATABASE test_db; 50 | ``` 51 | 52 | Next, create a MySQL user and give him/her the permissions : 53 | ```bash 54 | CREATE USER 'user'@'localhost' IDENTIFIED BY 'strongpassword'; 55 | GRANT ALL PRIVILEGES ON test_db.* TO 'user'@'localhost'; 56 | FLUSH PRIVILEGES; 57 | EXIT; 58 | ``` 59 | 60 | MySQL URI to Connect with projects: 61 | ```bash 62 | mysql://user_name@localhost:3306/database_name #without password 63 | mysql://user_name:password@localhost:3306/database_name #with password 64 | ``` 65 | 66 | ### 4. Some key points : 67 | 68 | #### 1. User privileges : 69 | 70 | Avoid `GRANT ALL PRIVILEGES` unless necessary. Give priority to specific rights: 71 | ```bash 72 | GRANT SELECT, INSERT, UPDATE, DELETE ON test_db.* TO 'user'@'localhost'; 73 | ``` 74 | 75 | #### 2. Remote access : 76 | 77 | If the user needs remote access, replace 'localhost' with '%' (and configure the firewall): 78 | ```bash 79 | CREATE USER 'user'@'%' IDENTIFIED BY 'strongpassword'; 80 | ``` -------------------------------------------------------------------------------- /PostgreSQL_Setup_on_Vps.md: -------------------------------------------------------------------------------- 1 | ## Setup PostgreSQL on VPS 2 | 3 | ### 1. Update existing packages 4 | ```bash 5 | sudo apt update && sudo apt upgrade -y 6 | ``` 7 | 8 | ### 2. Install PostgreSQL 9 | ```bash 10 | sudo apt install postgresql postgresql-contrib -y 11 | ``` 12 | 13 | ### 3. Check PostgreSQL service 14 | ```bash 15 | sudo systemctl status postgresql 16 | ``` 17 | 18 | ### 4. Access PostgreSQL 19 | ```bash 20 | sudo -u postgres psql 21 | ``` 22 | 23 | ### 5. Set administrator password (in PSQL shell) 24 | ```sql 25 | ALTER ROLE postgres WITH PASSWORD 'your_secure_password'; 26 | \q 27 | ``` 28 | 29 | ### 6. Modify authentication (optional) 30 | 31 | Edit the pg_hba.conf file: 32 | ```bash 33 | sudo nano /etc/postgresql/*/main/pg_hba.conf 34 | ``` 35 | 36 | Modify authentication methods (e.g. change “peer” to “md5” for local connections) 37 | 38 | ### 7. Configure remote access (optional) 39 | 40 | Edit postgresql.conf : 41 | ```bash 42 | sudo nano /etc/postgresql/*/main/postgresql.conf 43 | ``` 44 | 45 | Modify the line : 46 | ```conf 47 | listen_addresses = '*' 48 | ``` 49 | 50 | ### 8. Open firewall port 51 | ```bash 52 | sudo ufw allow 5432/tcp 53 | sudo ufw reload 54 | ``` 55 | 56 | ### 9. Restart PostgreSQL 57 | ```bash 58 | sudo systemctl restart postgresql 59 | ``` 60 | 61 | ### Safety tips : 62 | - Always use strong passwords 63 | - Limit remote access to necessary IPs 64 | - Create specific users rather than using 'postgres' 65 | - Update PostgreSQL regularly 66 | - Set up automatic backups 67 | 68 | To create a new user and database : 69 | ```bash 70 | sudo -u postgres createuser --interactive 71 | sudo -u postgres createdb database_name 72 | ``` -------------------------------------------------------------------------------- /Vercel_JSON_Config_For_MERN.md: -------------------------------------------------------------------------------- 1 | ## vercel.json 2 | 3 | Vercel json config for express backend 4 | ```bash 5 | { 6 | "version": 2, 7 | "builds": [ 8 | { 9 | "src": "server.js", 10 | "use": "@vercel/node", 11 | "config": { 12 | "includeFiles": [ 13 | "dist/**" 14 | ] 15 | } 16 | } 17 | ], 18 | "routes": [ 19 | { 20 | "src": "/(.*)", 21 | "dest": "server.js" 22 | } 23 | ] 24 | } 25 | ``` 26 | 27 | Vercel json config to support React Router in frontend 28 | ```bash 29 | { 30 | "rewrites": [ 31 | { 32 | "source": "/(.*)", 33 | "destination": "/" 34 | } 35 | ] 36 | } 37 | ``` 38 | --------------------------------------------------------------------------------