1. Home
  2. DEVOPS
  3. Server Setup
  4. Basic Setup

Basic Setup

🖥️ Complete Server Setup – বিস্তারিত গাইড (Part 1)

প্রতিটি step এ বিস্তারিত explanation — কেন কি করছি, কি কাজ করছে


📑 Table of Contents

  1. Basic Server Setup – বিস্তারিত
  2. Python Environment – বিস্তারিত
  3. Multiple Python Versions – বিস্তারিত
  4. Node.js Environment – বিস্তারিত
  5. PHP Environment – বিস্তারিত
  6. MySQL Database – বিস্তারিত
  7. PostgreSQL Database – বিস্তারিত
  8. MongoDB Database – বিস্তারিত
  9. Nginx Web Server – বিস্তারিত
  10. SSL Certificate – বিস্তারিত
  11. phpMyAdmin – বিস্তারিত

📑 Table of Contents

  1. Node.js Environment – বিস্তারিত
  2. PHP Environment – বিস্তারিত
  3. MySQL Database – বিস্তারিত
  4. PostgreSQL Database – বিস্তারিত
  5. MongoDB Database – বিস্তারিত

🔧 Basic Server Setup – বিস্তারিত

Step 1: System Update

sudo apt update

কি করছে:

  • Server এর package list update করছে
  • Ubuntu এর repository থেকে সব available packages এর list download করছে

কেন:

  • যাতে latest version এর software install করতে পারি
  • Security updates পেতে পারি

Output:

Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
...
Reading package lists... Done

sudo apt upgrade -y

কি করছে:

  • সব installed software কে latest version এ upgrade করছে
  • -y flag = automatically “yes” বলছে সব prompts এ

কেন:

  • Security vulnerabilities fix করার জন্য
  • Bug fixes পাওয়ার জন্য
  • Performance improvements এর জন্য

Output:

Reading package lists... Done
Building dependency tree
The following packages will be upgraded:
  base-files curl git ...

sudo apt install -y build-essential libssl-dev libffi-dev

কি করছে:

  • Development tools install করছে

প্রতিটি package কি করে:

  1. build-essential
    • কি: C/C++ compiler এবং build tools
    • কেন: Python packages compile করার জন্য
    • Example: যখন pip install numpy করব, তখন C code compile করতে হবে
  2. libssl-dev
    • কি: SSL/TLS library (HTTPS এর জন্য)
    • কেন: Secure connections এর জন্য
    • Example: Django app HTTPS এ run করতে হলে লাগবে
  3. libffi-dev
    • কি: Foreign Function Interface library
    • কেন: Python packages যা C libraries use করে তাদের জন্য
    • Example: cryptography package এর জন্য লাগে

Output:

Setting up build-essential (12.9ubuntu3) ...
Setting up libssl-dev (1.1.1-1ubuntu2.13) ...
Setting up libffi-dev (3.3-4) ...

sudo apt install -y curl wget git nano htop vim

প্রতিটি tool কি করে:

  1. curl
    • কি: Internet থেকে files download করার tool
    • কেন: Scripts download করতে, API test করতে
    • Example: curl https://example.com/script.sh
  2. wget
    • কি: আরেকটি download tool (curl এর alternative)
    • কেন: Backup হিসেবে, batch downloads এর জন্য
    • Example: wget https://example.com/file.zip
  3. git
    • কি: Version control system
    • কেন: Code manage করতে, GitHub থেকে clone করতে
    • Example: git clone https://github.com/user/repo.git
  4. nano
    • কি: Text editor (command line এ files edit করার জন্য)
    • কেন: Config files edit করতে
    • Example: nano /etc/nginx/nginx.conf
  5. htop
    • কি: System monitoring tool
    • কেন: CPU, RAM, processes দেখতে
    • Example: htop চালালে real-time monitoring দেখা যায়
  6. vim
    • কি: Advanced text editor
    • কেন: Power users এর জন্য
    • Example: vim /etc/nginx/nginx.conf

sudo apt install -y net-tools dnsutils

প্রতিটি tool কি করে:

  1. net-tools
    • কি: Network utilities (ifconfig, netstat, etc.)
    • কেন: Network troubleshooting এর জন্য
    • Example: netstat -tuln দিয়ে open ports দেখা যায়
  2. dnsutils
    • কি: DNS utilities (nslookup, dig, etc.)
    • কেন: DNS troubleshooting এর জন্য
    • Example: nslookup example.com দিয়ে DNS check করা যায়

Step 2: Firewall Configuration

sudo ufw enable

কি করছে:

  • Ubuntu Firewall (UFW) চালু করছে
  • এটি একটি security layer যা unauthorized access block করে

কেন:

  • Server কে protect করার জন্য
  • শুধুমাত্র necessary ports allow করার জন্য

Output:

Firewall is active and enabled on system startup

sudo ufw allow 22/tcp

কি করছে:

  • Port 22 (SSH) এ access allow করছে
  • TCP protocol এ

কেন:

  • যাতে SSH দিয়ে server এ connect করতে পারি
  • এটি না করলে SSH access block হয়ে যাবে

Port numbers কি:

  • Port = একটি virtual “door” যার মাধ্যমে data আসে যায়
  • Port 22 = SSH (Secure Shell)
  • Port 80 = HTTP (Web)
  • Port 443 = HTTPS (Secure Web)
  • Port 3306 = MySQL
  • Port 5432 = PostgreSQL

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

কি করছে:

  • Port 80 (HTTP) allow করছে
  • Port 443 (HTTPS) allow করছে

কেন:

  • Website visitors যাতে access করতে পারে
  • HTTP = unsecure web traffic
  • HTTPS = secure web traffic

sudo ufw status

কি করছে:

  • Firewall এর current status দেখাচ্ছে

Output:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

Step 3: Create Application Directory

sudo mkdir -p /var/www
sudo mkdir -p /var/www/apps

কি করছে:

  • /var/www directory তৈরি করছে (web applications এর জন্য standard location)
  • /var/www/apps subdirectory তৈরি করছে (আমাদের apps রাখার জন্য)
  • -p flag = parent directories automatically তৈরি করবে

কেন:

  • Web applications এর জন্য একটি organized structure থাকা উচিত
  • /var/www = Linux এর standard location web files এর জন্য

Directory structure:

/var/www/
├── apps/
   ├── python-app/
   ├── node-app/
   └── php-app/

sudo chown -R $USER:$USER /var/www
chmod -R 755 /var/www

কি করছে:

  • /var/www এর ownership change করছে
  • Permissions set করছে

বিস্তারিত:

  1. chown -R $USER:$USER /var/www
    • chown = change owner
    • -R = recursive (সব subdirectories এও apply করবে)
    • $USER:$USER = current user কে owner বানাচ্ছে
    • কেন: যাতে sudo ছাড়াই files edit করতে পারি
  2. chmod -R 755 /var/www
    • chmod = change mode (permissions)
    • -R = recursive
    • 755 = permissions:
      • Owner: read, write, execute (7)
      • Group: read, execute (5)
      • Others: read, execute (5)
    • কেন: Security এবং accessibility balance করার জন্য

🐍 Python Environment – বিস্তারিত

Step 1: Python 3.11 Installation

sudo apt install -y python3.11 python3.11-venv python3.11-dev

প্রতিটি package কি করে:

  1. python3.11
    • কি: Python interpreter (Python code run করার জন্য)
    • কেন: Python applications run করতে হলে লাগে
    • Example: python3.11 script.py দিয়ে script run করা যায়
  2. python3.11-venv
    • কি: Virtual Environment tool
    • কেন: Isolated Python environments তৈরি করার জন্য
    • কেন এটা important:Project A: Django 4.0, requests 2.28 Project B: Django 3.0, requests 2.25 Virtual environment ছাড়া conflict হবে Virtual environment দিয়ে দুটো আলাদা থাকবে
  3. python3.11-dev
    • কি: Development headers এবং libraries
    • কেন: Python packages compile করার জন্য
    • Example: pip install numpy করলে C code compile করতে হবে

python3.11 --version

কি করছে:

  • Python version check করছে

Output:

Python 3.11.0

Step 2: Virtual Environment Setup

mkdir -p /var/www/apps/python-app
cd /var/www/apps/python-app

কি করছে:

  • Python app এর জন্য directory তৈরি করছে
  • সেই directory তে যাচ্ছি

python3.11 -m venv venv

কি করছে:

  • Virtual environment তৈরি করছে
  • venv নামে একটি folder তৈরি হবে

কেন:

  • প্রতিটি project এর জন্য isolated environment থাকবে
  • একটি project এর packages অন্য project কে affect করবে না

Virtual environment এর structure:

venv/
├── bin/
   ├── python
   ├── pip
   ├── activate
   └── ...
├── lib/
   └── python3.11/
       └── site-packages/
           └── (installed packages এখানে থাকবে)
└── pyvenv.cfg

source venv/bin/activate

কি করছে:

  • Virtual environment activate করছে
  • এখন যা install করব তা শুধু এই environment এ থাকবে

কেন:

  • Global Python environment কে protect করার জন্য
  • Project-specific packages install করার জন্য

কিভাবে বুঝবো activate হয়েছে:

(venv) user@server:/var/www/apps/python-app$
  • Prompt এর শুরুতে (venv) দেখা যাবে

pip install --upgrade pip
pip install django gunicorn flask fastapi

কি করছে:

  • pip upgrade করছে (latest version)
  • Python packages install করছে

প্রতিটি package কি করে:

  1. django
    • কি: Full-featured web framework
    • কেন: Complex web applications তৈরি করতে
    • Features: ORM, Admin panel, Authentication, etc.
  2. gunicorn
    • কি: Application server
    • কেন: Django app কে production environment এ run করতে
    • কেন এটা লাগে: Django development server production এ use করা যায় না
  3. flask
    • কি: Lightweight web framework
    • কেন: Simple web applications তৈরি করতে
    • Django vs Flask: Django = full-featured, Flask = lightweight
  4. fastapi
    • কি: Modern API framework
    • কেন: REST APIs তৈরি করতে
    • Features: Automatic documentation, async support

Step 3: Gunicorn Setup – বিস্তারিত

কেন Gunicorn লাগে?

Django Development Server:
- Single-threaded
- Production  use করা unsafe
- Performance কম

Gunicorn (Production Server):
- Multi-worker
- Concurrent requests handle করতে পারে
- Production-ready
- Nginx এর সাথে কাজ করে

Architecture:

Internet
   
Nginx (Web Server)
   
Gunicorn Socket (/run/gunicorn.sock)
   
Gunicorn Workers (3টি process)
   
Django Application

Socket File কেন লাগে?

sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

কি করছে:

  • Socket file তৈরি করছে
  • এটি একটি virtual “pipe” যার মাধ্যমে Nginx এবং Gunicorn communicate করবে

কেন Socket লাগে:

  • Nginx এবং Gunicorn এর মধ্যে communication এর জন্য
  • TCP port এর চেয়ে socket বেশি efficient
  • Security: localhost এ শুধুমাত্র

Socket vs TCP Port:

Socket (/run/gunicorn.sock):
- Faster
- More secure
- Local communication only

TCP Port (127.0.0.1:8000):
- Slower
- Less secure
- Can be accessed from network

Service File কেন লাগে?

sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
Type=notify
User=www-data
Group=www-data
WorkingDirectory=/var/www/apps/python-app
ExecStart=/var/www/apps/python-app/venv/bin/gunicorn \
    --workers 3 \
    --worker-class sync \
    --bind unix:/run/gunicorn.sock \
    --timeout 30 \
    app:app
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

প্রতিটি line কি করে:

  1. [Unit]Description=gunicorn daemon
    • Service এর description
  2. Requires=gunicorn.socket
    • Socket file এর উপর depend করছে
    • Socket first start হবে, তারপর service
  3. After=network.target
    • Network ready হওয়ার পর start হবে
  4. [Service]Type=notify
    • Service type = notify (systemd কে notify করবে যখন ready হবে)
  5. User=www-data, Group=www-data
    • Gunicorn www-data user দিয়ে run হবে
    • কেন: Security (root দিয়ে run করা unsafe)
  6. WorkingDirectory=/var/www/apps/python-app
    • Gunicorn এই directory থেকে run হবে
  7. ExecStart=…
    • Gunicorn কে কিভাবে start করতে হবে তা বলছে
  8. –workers 3
    • 3টি worker process তৈরি করবে
    • একসাথে 3টি request handle করতে পারবে
    • Formula: (2 × CPU cores) + 1
    • Example: 2 core CPU = (2 × 2) + 1 = 5 workers
  9. –worker-class sync
    • Synchronous workers (একটি request এর পর একটি)
    • Alternative: async (gevent, uvicorn)
  10. –bind unix:/run/gunicorn.sock
    • Socket file এ bind করছে
  11. –timeout 30
    • 30 সেকেন্ড timeout
    • এর বেশি সময় লাগলে worker restart হবে
  12. app:app
    • app.py ফাইলের app object run করছে
    • Format: module:variable
  13. Restart=always
    • Gunicorn crash হলে automatically restart হবে
  14. RestartSec=10
    • 10 সেকেন্ড পর restart হবে
  15. [Install]WantedBy=multi-user.target
    • Server boot হলে automatically start হবে

Enable এবং Start করা:

sudo systemctl daemon-reload

কি করছে: systemd কে নতুন service file সম্পর্কে জানাচ্ছে

sudo systemctl enable gunicorn.socket gunicorn.service

কি করছে: Server boot হলে automatically start হবে

sudo systemctl start gunicorn.socket gunicorn.service

কি করছে: এখনই start করছে

sudo systemctl status gunicorn.service

কি করছে: Status check করছে

Output:

 gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-02-03 10:30:00 UTC; 5s ago

🐍 Multiple Python Versions – বিস্তারিত

কেন Multiple Python Versions লাগে?

Scenario 1:
Project A: Python 3.9 (legacy project)
Project B: Python 3.11 (new project)
Project C: Python 3.12 (latest)

একটি server  সব version থাকতে পারে
প্রতিটি project এর জন্য আলাদা version use করতে পারবেন

Step 1: Multiple Python Versions Install করা

Python 3.9 Install করুন

sudo apt install -y python3.9 python3.9-venv python3.9-dev

কি করছে:

  • Python 3.9 install করছে

Python 3.10 Install করুন

sudo apt install -y python3.10 python3.10-venv python3.10-dev

কি করছে:

  • Python 3.10 install করছে

Python 3.11 Install করুন

sudo apt install -y python3.11 python3.11-venv python3.11-dev

কি করছে:

  • Python 3.11 install করছে

Python 3.12 Install করুন

sudo apt install -y python3.12 python3.12-venv python3.12-dev

কি করছে:

  • Python 3.12 install করছে

Step 2: সব Versions Check করা

python3.9 --version
python3.10 --version
python3.11 --version
python3.12 --version

Output:

Python 3.9.18
Python 3.10.13
Python 3.11.7
Python 3.12.1

Step 3: Default Python Version Set করা

Current default check করুন

python3 --version

Output:

Python 3.11.7

Default version change করুন (update-alternatives)

# Python 3.9 কে priority 1 দিয়ে add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1

# Python 3.10 কে priority 2 দিয়ে add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2

# Python 3.11 কে priority 3 দিয়ে add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 3

# Python 3.12 কে priority 4 দিয়ে add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 4

কি করছে:

  • update-alternatives = symbolic link manage করার tool
  • /usr/bin/python3 = যেখানে link তৈরি হবে
  • python3 = link এর নাম
  • /usr/bin/python3.X = actual binary path
  • Priority = যত বেশি priority, তত বেশি preference

কেন এটা কাজ করে:

/usr/bin/python3  /usr/bin/python3.11 (default)

যখন `python3` command run করবেন
তখন python3.11 run হবে

Default version select করুন (interactive)

sudo update-alternatives --config python3

Output:

There are 4 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path             Priority   Status
----------------------------------------------------
* 0            /usr/bin/python3.12   4       auto mode
  1            /usr/bin/python3.9    1       manual mode
  2            /usr/bin/python3.10   2       manual mode
  3            /usr/bin/python3.11   3       manual mode
  4            /usr/bin/python3.12   4       manual mode

Press <enter> to keep the current choice[*], or type selection number:

কি করছে:

  • সব available versions দেখাচ্ছে
  • আপনি যেকোনো একটি select করতে পারেন
  • * = currently selected

Example:

আপনি 3 type করলে Python 3.11 default হবে

Step 4: প্রতিটি Project এর জন্য Specific Version Use করা

Project A (Python 3.9 use করতে চান)

mkdir -p /var/www/apps/project-a
cd /var/www/apps/project-a

# Python 3.9 দিয়ে virtual environment তৈরি করুন
python3.9 -m venv venv

# Activate করুন
source venv/bin/activate

# Check করুন
python --version

Output:

Python 3.9.18

কি হয়েছে:

  • Virtual environment এ Python 3.9 use হচ্ছে
  • Global default যাই হোক না কেন, এই project এ 3.9 থাকবে

Project B (Python 3.11 use করতে চান)

mkdir -p /var/www/apps/project-b
cd /var/www/apps/project-b

# Python 3.11 দিয়ে virtual environment তৈরি করুন
python3.11 -m venv venv

# Activate করুন
source venv/bin/activate

# Check করুন
python --version

Output:

Python 3.11.7

Project C (Python 3.12 use করতে চান)

mkdir -p /var/www/apps/project-c
cd /var/www/apps/project-c

# Python 3.12 দিয়ে virtual environment তৈরি করুন
python3.12 -m venv venv

# Activate করুন
source venv/bin/activate

# Check করুন
python --version

Output:

Python 3.12.1

Step 5: Default Python Version Verify করা

python3 --version

Output:

Python 3.11.7

কি হয়েছে:

  • Global default Python 3.11
  • কিন্তু প্রতিটি project এর virtual environment এ নিজস্ব version আছে

🎯 Summary: Multiple Python Versions

Installation:

# সব versions install করুন
sudo apt install -y python3.9 python3.9-venv python3.9-dev
sudo apt install -y python3.10 python3.10-venv python3.10-dev
sudo apt install -y python3.11 python3.11-venv python3.11-dev
sudo apt install -y python3.12 python3.12-venv python3.12-dev

Default Version Set করুন:

# সব versions add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 4

# Default select করুন
sudo update-alternatives --config python3

প্রতিটি Project এ Specific Version Use করুন:

# Project A (Python 3.9)
python3.9 -m venv venv

# Project B (Python 3.11)
python3.11 -m venv venv

# Project C (Python 3.12)
python3.12 -m venv venv

💡 Best Practices

1. Default Version কি হওয়া উচিত?

সবচেয়ে বেশি use করা version কে default করুন
Example: যদি 80% projects Python 3.11 use করে
তাহলে 3.11 কে default করুন

2. Virtual Environment এ Version Check করুন

cd /var/www/apps/project-a
source venv/bin/activate

# এটা project-specific version দেখাবে
python --version

# এটা global default দেখাবে
python3 --version

3. pip Version Check করুন

# Virtual environment activate করার পর
pip --version

# Output:
# pip 23.0.1 from /var/www/apps/project-a/venv/lib/python3.9/site-packages/python3.9

কি দেখাচ্ছে:

  • pip version
  • কোন Python version এর জন্য
  • কোন directory থেকে

🔧 Common Commands

সব Python Versions দেখুন

ls /usr/bin/python*

Output:

/usr/bin/python3
/usr/bin/python3.9
/usr/bin/python3.10
/usr/bin/python3.11
/usr/bin/python3.12

Current Default Check করুন

python3 --version

Virtual Environment এর Version Check করুন

cd /var/www/apps/project-a
source venv/bin/activate
python --version

Default Version Change করুন

sudo update-alternatives --config python3

🚨 Troubleshooting

Python version not found?

# Install করুন
sudo apt install -y python3.X python3.X-venv python3.X-dev

Virtual environment এ wrong version?

# Virtual environment delete করুন
rm -rf venv

# সঠিক version দিয়ে recreate করুন
python3.X -m venv venv
source venv/bin/activate

Default version change হচ্ছে না?

# update-alternatives list দেখুন
sudo update-alternatives --list python3

# সঠিক priority দিয়ে add করুন
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.X 10

চমৎকার! নিচে Part 2 আগের মতোই রাখা হলো, শুধু Table of Contents এখন 12 থেকে শুরু হয়েছে — আপনার Part 1 এর সাথে সিরিয়াল ঠিক রাখতে।


🖥️ Complete Server Setup – বিস্তারিত গাইড (Part 2)

Node.js, PHP, Databases – বিস্তারিত explanation



🟢 Node.js Environment – বিস্তারিত

Step 1: Node.js Installation

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

কি করছে:

  • NodeSource repository add করছে
  • Ubuntu এর default repository তে latest Node.js নেই, তাই external repository লাগে

বিস্তারিত:

  • curl = internet থেকে script download করছে
  • -fsSL flags:
    • -f = fail silently
    • -s = silent mode
    • -S = show errors
    • -L = follow redirects
  • | sudo -E bash - = script কে bash দিয়ে run করছে

কেন:

  • NodeSource সবসময় latest Node.js version maintain করে

sudo apt install -y nodejs

কি করছে:

  • Node.js install করছে

কি install হয়:

  • node = JavaScript runtime
  • npm = Node Package Manager (Python এর pip এর মতো)

node --version
npm --version

Output:

v20.10.0
9.2.0

sudo npm install -g pm2

কি করছে:

  • PM2 globally install করছে
  • -g = global (সব projects এ use করতে পারবে)

PM2 কি:

  • Process Manager for Node.js
  • Node.js apps কে background এ run রাখে
  • App crash হলে automatically restart করে
  • Logs manage করে

কেন PM2 লাগে:

Node.js Development:
node app.js
- Terminal close হলে app বন্ধ হয়ে যায়
- Crash হলে restart হয় না

PM2 দিয়ে:
pm2 start app.js
- Background এ চলে
- Terminal close হলেও চলতে থাকে
- Crash হলে automatically restart হয়
- Logs save হয়

Step 2: Node.js App Setup

mkdir -p /var/www/apps/node-app
cd /var/www/apps/node-app
npm init -y

কি করছে:

  • Node.js app এর জন্য directory তৈরি করছে
  • npm init -y = package.json তৈরি করছে (automatically)

package.json কি:

{
  "name": "node-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

কেন লাগে:

  • Project এর metadata store করে
  • Dependencies list করে
  • Scripts define করে

npm install express

কি করছে:

  • Express framework install করছে

Express কি:

  • Node.js এর জন্য web framework
  • Django/Flask এর মতো

কি হয়:

  • node_modules/ folder তৈরি হয় (সব packages এখানে থাকে)
  • package-lock.json তৈরি হয় (exact versions record করে)

cat > app.js <<'EOF'
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Node.js!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
EOF

কি করছে:

  • app.js ফাইল তৈরি করছে
  • Express app define করছে

কোড বুঝা:

const express = require('express');
// Express module import করছে

const app = express();
// Express app তৈরি করছে

app.get('/', (req, res) => {
  // GET request handle করছে
  // '/' = root path (example.com/)
  res.send('Hello from Node.js!');
  // Response পাঠাচ্ছে
});

app.listen(3000, () => {
  // Port 3000 এ listen করছে
  console.log('Server running on port 3000');
});

Step 3: PM2 Setup

pm2 start app.js --name "node-app"

কি করছে:

  • app.js কে PM2 দিয়ে start করছে
  • --name "node-app" = একটি নাম দিচ্ছে

কেন:

  • Background এ চলবে
  • Crash হলে restart হবে
  • Logs save হবে

pm2 save

কি করছে:

  • PM2 configuration save করছে

কেন:

  • Server restart হলেও app automatically start হবে

pm2 startup

কি করছে:

  • PM2 কে system startup এ add করছে

Output:

[PM2] Init system found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user

PM2 Common Commands

# List running apps
pm2 list

# View logs
pm2 logs node-app

# Restart app
pm2 restart node-app

# Stop app
pm2 stop node-app

# Delete app
pm2 delete node-app

# Monitor
pm2 monit

🐘 PHP Environment – বিস্তারিত

Step 1: PHP Installation

sudo apt install -y php8.2 php8.2-fpm php8.2-cli php8.2-common

প্রতিটি package কি করে:

  1. php8.2
    • কি: PHP interpreter
    • কেন: PHP code run করার জন্য
  2. php8.2-fpm
    • কি: FastCGI Process Manager
    • কেন: Nginx এর সাথে কাজ করার জন্য
    • কেন এটা লাগে:Apache: mod_php (built-in) Nginx: FPM (separate process) Nginx PHP code directly run করতে পারে না FPM এর মাধ্যমে communicate করে
  3. php8.2-cli
    • কি: Command Line Interface
    • কেন: Terminal থেকে PHP scripts run করতে
  4. php8.2-common
    • কি: Common libraries
    • কেন: Basic functionality এর জন্য

sudo apt install -y php8.2-mysql php8.2-pgsql php8.2-sqlite3

কি করছে:

  • Database extensions install করছে

প্রতিটি extension কি করে:

  1. php8.2-mysql
    • কি: MySQL driver
    • কেন: PHP থেকে MySQL database access করতে
  2. php8.2-pgsql
    • কি: PostgreSQL driver
    • কেন: PHP থেকে PostgreSQL database access করতে
  3. php8.2-sqlite3
    • কি: SQLite driver
    • কেন: PHP থেকে SQLite database access করতে

sudo apt install -y php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-bcmath

প্রতিটি extension কি করে:

  1. php8.2-curl
    • কি: cURL library
    • কেন: PHP থেকে HTTP requests করতে
    • Example: curl_init(), curl_exec()
  2. php8.2-gd
    • কি: Graphics library
    • কেন: Image manipulation করতে
    • Example: Image resize, watermark add করা
  3. php8.2-mbstring
    • কি: Multibyte string functions
    • কেন: Unicode characters handle করতে
    • Example: Bengali, Arabic text
  4. php8.2-xml
    • কি: XML parser
    • কেন: XML files parse করতে
    • Example: RSS feeds, SOAP
  5. php8.2-zip
    • কি: ZIP archive support
    • কেন: ZIP files create/extract করতে
  6. php8.2-bcmath
    • কি: Arbitrary precision math
    • কেন: Precise calculations করতে
    • Example: Financial calculations

Step 2: PHP Configuration

sudo nano /etc/php/8.2/fpm/php.ini

Important settings:

upload_max_filesize = 100M
  • Maximum file upload size = 100 MB
post_max_size = 100M
  • Maximum POST data size = 100 MB
memory_limit = 256M
  • PHP script এর জন্য maximum memory = 256 MB
max_execution_time = 300
  • Script execution time limit = 300 seconds (5 minutes)

Step 3: PHP-FPM Setup

sudo nano /etc/php/8.2/fpm/pool.d/www.conf

Important settings:

user = www-data
group = www-data
  • PHP-FPM www-data user দিয়ে run হবে
listen = /run/php/php8.2-fpm.sock
  • Socket file path
  • Nginx এই socket এর মাধ্যমে PHP-FPM এর সাথে communicate করবে

sudo systemctl restart php8.2-fpm

কি করছে:

  • PHP-FPM restart করছে
  • Configuration changes apply হবে

Step 4: Create PHP Application

sudo mkdir -p /var/www/apps/php-app
sudo chown -R www-data:www-data /var/www/apps/php-app

কি করছে:

  • PHP app directory তৈরি করছে
  • Ownership www-data কে দিচ্ছে

কেন:

  • PHP-FPM www-data user দিয়ে run হয়
  • তাই files এর ownership www-data হতে হবে

sudo nano /var/www/apps/php-app/index.php
<?php
echo "Hello from PHP!";
phpinfo();
?>

কি করছে:

  • Simple PHP file তৈরি করছে
  • phpinfo() = PHP configuration দেখাবে

🗄️ MySQL Database – বিস্তারিত

Step 1: MySQL Installation

sudo apt install -y mysql-server

কি করছে:

  • MySQL Server install করছে

MySQL কি:

  • Relational database
  • Data structured tables এ store হয়
  • SQL language use করে

sudo mysql_secure_installation

কি করছে:

  • MySQL কে secure করছে

Prompts:

  1. Validate Password Plugin?
    • Strong password enforce করবে
    • Answer: y (yes)
  2. Remove anonymous users?
    • Anonymous access remove করবে
    • Answer: y
  3. Disable root remote login?
    • Root কে শুধু localhost থেকে access করতে দেবে
    • Answer: y
  4. Remove test database?
    • Test database delete করবে
    • Answer: y
  5. Reload privilege tables?
    • Changes apply করবে
    • Answer: y

Step 2: MySQL Configuration

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Important settings:

bind-address = 127.0.0.1
  • MySQL শুধু localhost এ listen করবে
  • Remote access block হবে (security)
port = 3306
  • MySQL port number
max_connections = 1000
  • Maximum concurrent connections
default-storage-engine = InnoDB
  • Default storage engine
  • InnoDB = transactions support করে

sudo systemctl restart mysql

কি করছে:

  • MySQL restart করছে
  • Configuration changes apply হবে

Step 3: Create Database and User

sudo mysql -u root -p

কি করছে:

  • MySQL shell এ login করছে
  • -u root = root user
  • -p = password prompt করবে

CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

কি করছে:

  • Database তৈরি করছে

বিস্তারিত:

  • CHARACTER SET utf8mb4 = UTF-8 encoding (Bengali, Arabic, etc. support)
  • COLLATE utf8mb4_unicode_ci = Case-insensitive comparison

CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password_123';

কি করছে:

  • Database user তৈরি করছে

বিস্তারিত:

  • 'myapp_user'@'localhost' = username@host
  • localhost = শুধু local machine থেকে access
  • IDENTIFIED BY = password set করছে

GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;

কি করছে:

  • User কে database এর সব permissions দিচ্ছে
  • FLUSH PRIVILEGES = changes apply করছে

Step 4: MySQL Backup and Restore

mysqldump -u myapp_user -p myapp_db > backup.sql

কি করছে:

  • Database backup নিচ্ছে
  • mysqldump = database export করার tool
  • > backup.sql = file এ save করছে

mysql -u myapp_user -p myapp_db < backup.sql

কি করছে:

  • Backup থেকে restore করছে
  • < backup.sql = file থেকে read করছে

🐘 PostgreSQL Database – বিস্তারিত

Step 1: PostgreSQL Installation

sudo apt install -y postgresql postgresql-contrib

প্রতিটি package কি করে:

  1. postgresql
    • কি: PostgreSQL database server
    • কেন: Advanced relational database
  2. postgresql-contrib
    • কি: Additional modules
    • কেন: Extra functionality

MySQL vs PostgreSQL:

MySQL:
- Simple
- Fast
- Widely used

PostgreSQL:
- Advanced features
- Better for complex queries
- More reliable

Step 2: PostgreSQL Configuration

sudo nano /etc/postgresql/15/main/postgresql.conf

Important settings:

listen_addresses = 'localhost'
  • PostgreSQL শুধু localhost এ listen করবে
port = 5432
  • PostgreSQL port number
max_connections = 100
  • Maximum concurrent connections

Step 3: Create Database and User

sudo -u postgres psql

কি করছে:

  • PostgreSQL shell এ login করছে
  • sudo -u postgres = postgres user দিয়ে run করছে

CREATE DATABASE myapp_db;

কি করছে:

  • Database তৈরি করছে

CREATE USER myapp_user WITH PASSWORD 'strong_password_123';

কি করছে:

  • User তৈরি করছে

ALTER ROLE myapp_user SET client_encoding TO 'utf8';
ALTER ROLE myapp_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE myapp_user SET default_transaction_deferrable TO on;
ALTER ROLE myapp_user SET default_transaction_read_only TO off;
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp_user;

কি করছে:

  • User এর settings configure করছে
  • Permissions দিচ্ছে

🍃 MongoDB Database – বিস্তারিত

Step 1: MongoDB Installation

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -

কি করছে:

  • MongoDB GPG key add করছে
  • Package verification এর জন্য

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

কি করছে:

  • MongoDB repository add করছে

sudo apt update
sudo apt install -y mongodb-org

কি করছে:

  • MongoDB install করছে

Step 2: MongoDB Configuration

sudo nano /etc/mongod.conf

Important settings:

bindIp: 127.0.0.1
  • MongoDB শুধু localhost এ listen করবে
port: 27017
  • MongoDB port number
storage:
  dbPath: /var/lib/mongodb
  • Data storage location

Step 3: Create Database and User

mongosh

কি করছে:

  • MongoDB shell এ login করছে

use admin
db.createUser({
  user: "admin",
  pwd: "admin_password_123",
  roles: ["root"]
})

কি করছে:

  • Admin user তৈরি করছে

use myapp_db
db.createUser({
  user: "myapp_user",
  pwd: "strong_password_123",
  roles: ["readWrite", "dbAdmin"]
})

কি করছে:

  • Database user তৈরি করছে

দারুণ! নিচে Part 3 আগের মতোই রাখা হলো, শুধু Table of Contents এখন 17 থেকে শুরু হয়েছে — যাতে আপনার Part 1 (1–11) ও Part 2 (12–16) এর সাথে সিরিয়াল ঠিক থাকে।


🖥️ Complete Server Setup – বিস্তারিত গাইড (Part 3)

Nginx, SSL, Django, Node.js, FastAPI, WordPress, Laravel Deploy


📑 Table of Contents

  1. Nginx Web Server – বিস্তারিত
  2. SSL Certificate – বিস্তারিত
  3. Django App Deploy
  4. Node.js App Deploy
  5. FastAPI App Deploy
  6. WordPress Deploy
  7. Laravel Deploy

🌐 Nginx Web Server – বিস্তারিত

Nginx কি এবং কেন লাগে?

Architecture:

Internet
   ↓
Nginx (Web Server)
   ↓
Backend (Django/Node.js/PHP)
   ↓
Database

Nginx এর কাজ:

  1. HTTP requests receive করা
  2. Static files serve করা (images, CSS, JS)
  3. Dynamic requests backend এ forward করা
  4. Response client এ পাঠানো
  5. Load balancing
  6. SSL/TLS termination

কেন Nginx লাগে:

  • Django/Node.js development server production এ use করা যায় না
  • Nginx production-ready web server
  • Performance, security, scalability

Step 1: Nginx Installation

sudo apt install -y nginx

কি করছে:

  • Nginx install করছে

nginx -v
sudo systemctl status nginx

কি করছে:

  • Nginx version check করছে
  • Status check করছে

Step 2: Nginx Configuration for Django

sudo nano /etc/nginx/sites-available/django-app
upstream gunicorn {
    server unix:/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    client_max_body_size 20M;

    location /static/ {
        alias /var/www/apps/django-app/staticfiles/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    location /media/ {
        alias /var/www/apps/django-app/media/;
        expires 7d;
    }

    location / {
        proxy_pass http://gunicorn;
        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;
    }
}

প্রতিটি section বুঝা:

upstream gunicorn

upstream gunicorn {
    server unix:/run/gunicorn.sock fail_timeout=0;
}
  • Gunicorn server এর reference define করছে
  • unix:/run/gunicorn.sock = socket file path
  • fail_timeout=0 = fail হলে retry করবে

server block

server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    client_max_body_size 20M;
  • listen 80 = HTTP port
  • server_name = domain names
  • client_max_body_size 20M = maximum upload size

Static files location

location /static/ {
    alias /var/www/apps/django-app/staticfiles/;
    expires 30d;
    add_header Cache-Control "public, immutable";
}
  • /static/ requests এ static files serve করছে
  • alias = actual file path
  • expires 30d = browser cache 30 days
  • কেন: Static files frequently change হয় না, তাই cache করা যায়

Media files location

location /media/ {
    alias /var/www/apps/django-app/media/;
    expires 7d;
}
  • /media/ requests এ media files serve করছে
  • expires 7d = browser cache 7 days

Dynamic requests location

location / {
    proxy_pass http://gunicorn;
    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;
}
  • সব অন্যান্য requests Gunicorn এ forward করছে
  • proxy_pass http://gunicorn = upstream এ পাঠাচ্ছে
  • Headers set করছে যাতে Django জানে real client কে request এসেছে

Headers কেন লাগে:

Without headers:
- Django দেখে request localhost থেকে এসেছে
- Client IP wrong থাকে
- HTTPS detection fail হয়

With headers:
- Django জানে real client IP
- HTTPS properly detect হয়

Step 3: Nginx Configuration for Node.js

sudo nano /etc/nginx/sites-available/node-app
upstream node_app {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    client_max_body_size 20M;

    location / {
        proxy_pass http://node_app;
        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;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Django এর সাথে পার্থক্য:

  1. upstreamserver 127.0.0.1:3000;
    • Socket এর বদলে TCP port
    • Node.js PM2 port 3000 এ listen করছে
  2. Extra headersproxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";
    • WebSocket support এর জন্য
    • Node.js apps often WebSocket use করে

Step 4: Nginx Configuration for PHP

sudo nano /etc/nginx/sites-available/php-app
server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    root /var/www/apps/php-app;
    index index.php index.html;
    client_max_body_size 100M;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

বিস্তারিত:

root এবং index

root /var/www/apps/php-app;
index index.php index.html;
  • root = document root
  • index = default files (order এ search করবে)

PHP location

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
  • ~ \.php$ = regex (.php এ ending files)
  • fastcgi_pass = PHP-FPM socket এ পাঠাচ্ছে
  • SCRIPT_FILENAME = PHP script এর full path

.htaccess deny

location ~ /\.ht {
    deny all;
}
  • .htaccess files access block করছে
  • Security (Apache config files expose হবে না)

Step 5: Enable Nginx Sites

sudo ln -s /etc/nginx/sites-available/django-app /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

কি করছে:

  • Django app enable করছে
  • Default site disable করছে

sudo nginx -t

কি করছে:

  • Nginx configuration test করছে
  • Syntax errors check করছে

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

sudo systemctl restart nginx

কি করছে:

  • Nginx restart করছে
  • Configuration changes apply হবে

🔒 SSL Certificate – বিস্তারিত

SSL কি এবং কেন লাগে?

HTTP (Unsecure):
Client ←→ Server
Data plain text এ transmit হয়
Hacker intercept করতে পারে

HTTPS (Secure):
Client ←→ Server (Encrypted)
Data encrypted থাকে
Hacker intercept করলেও পড়তে পারবে না

SSL Certificate:

  • Server এর identity verify করে
  • Encryption enable করে
  • Browser এ green lock দেখায়

Step 1: Certbot Installation

sudo apt install -y certbot python3-certbot-nginx

কি করছে:

  • Certbot install করছে (Let’s Encrypt client)
  • Nginx plugin install করছে

Step 2: Generate SSL Certificate

sudo certbot certonly --nginx -d your-domain.com -d www.your-domain.com

কি করছে:

  • SSL certificate generate করছে
  • Let’s Encrypt থেকে

Prompts:

  1. Email address
    • Renewal reminders পাবেন
  2. Agree to terms
    • Let’s Encrypt terms accept করতে হবে
  3. Share email
    • Optional

Output:

Congratulations! Your certificate has been issued.
Certificate is saved at: /etc/letsencrypt/live/your-domain.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your-domain.com/privkey.pem

Step 3: Update Nginx for HTTPS

sudo nano /etc/nginx/sites-available/django-app
upstream gunicorn {
    server unix:/run/gunicorn.sock fail_timeout=0;
}

# HTTP to HTTPS redirect
server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    return 301 https://$server_name$request_uri;
}

# HTTPS server
server {
    listen 443 ssl http2;
    server_name your-domain.com www.your-domain.com;
    client_max_body_size 20M;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location /static/ {
        alias /var/www/apps/django-app/staticfiles/;
        expires 30d;
    }

    location / {
        proxy_pass http://gunicorn;
        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;
    }
}

বিস্তারিত:

HTTP to HTTPS redirect

server {
    listen 80;
    server_name your-domain.com www.your-domain.com;
    return 301 https://$server_name$request_uri;
}
  • HTTP requests HTTPS এ redirect করছে
  • 301 = permanent redirect

HTTPS server

listen 443 ssl http2;
  • Port 443 (HTTPS)
  • ssl = SSL enabled
  • http2 = HTTP/2 protocol (faster)

SSL certificates

ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
  • Certificate এবং private key path

SSL protocols

ssl_protocols TLSv1.2 TLSv1.3;
  • TLS 1.2 এবং 1.3 use করছে
  • Older versions disable করছে (security)

SSL ciphers

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
  • Strong ciphers use করছে
  • Weak ciphers disable করছে

Step 4: Auto-Renewal Setup

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

কি করছে:

  • Certbot timer enable করছে
  • Automatic renewal setup হচ্ছে

কেন:

  • Let’s Encrypt certificates 90 days valid
  • Auto-renewal 30 days পর start হয়

sudo certbot renew --dry-run

কি করছে:

  • Renewal test করছে
  • Actual renewal না করে

🚀 Django App Deploy

Step 1: Project Setup

cd /var/www/apps/django-app
source venv/bin/activate

কি করছে:

  • Django app directory তে যাচ্ছি
  • Virtual environment activate করছি

Step 2: Install Dependencies

pip install -r requirements.txt

requirements.txt example:

Django==4.2.0
gunicorn==21.2.0
psycopg2-binary==2.9.0
python-decouple==3.8
Pillow==10.0.0

Step 3: Environment Variables

nano .env
DEBUG=False
SECRET_KEY=your-secret-key-here
ALLOWED_HOSTS=your-domain.com,www.your-domain.com
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
STATIC_ROOT=/var/www/apps/django-app/staticfiles
MEDIA_ROOT=/var/www/apps/django-app/media

Step 4: Database Setup

python manage.py migrate
python manage.py collectstatic --noinput
python manage.py createsuperuser

কি করছে:

  • Database migrations run করছে
  • Static files collect করছে
  • Admin user তৈরি করছে

Step 5: Start Gunicorn

sudo systemctl start gunicorn.socket gunicorn.service
sudo systemctl status gunicorn.service

কি করছে:

  • Gunicorn start করছে
  • Status check করছে

🟢 Node.js App Deploy

Step 1: Project Setup

cd /var/www/apps/node-app
npm install

কি করছে:

  • Dependencies install করছে

Step 2: Environment Variables

nano .env
NODE_ENV=production
PORT=3000
DATABASE_URL=mongodb://user:password@localhost:27017/dbname

Step 3: Start with PM2

pm2 start app.js --name "node-app"
pm2 save
pm2 startup

কি করছে:

  • PM2 দিয়ে app start করছে
  • Configuration save করছে
  • Startup script setup করছে

Step 4: Verify

pm2 list
pm2 logs node-app

⚡ FastAPI App Deploy

Step 1: Project Setup

cd /var/www/apps/fastapi-app
python3.11 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn

Step 2: Create FastAPI App

nano main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello from FastAPI"}

@app.get("/api/users/{user_id}")
def read_user(user_id: int):
    return {"user_id": user_id}

Step 3: Create Systemd Service

sudo nano /etc/systemd/system/fastapi.service
[Unit]
Description=FastAPI application
After=network.target

[Service]
Type=notify
User=www-data
Group=www-data
WorkingDirectory=/var/www/apps/fastapi-app
ExecStart=/var/www/apps/fastapi-app/venv/bin/uvicorn main:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Step 4: Start Service

sudo systemctl daemon-reload
sudo systemctl enable fastapi.service
sudo systemctl start fastapi.service

Step 5: Nginx Configuration

sudo nano /etc/nginx/sites-available/fastapi-app
upstream fastapi {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    server_name your-domain.com;
    
    location / {
        proxy_pass http://fastapi;
        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;
    }
}

📝 WordPress Deploy

Step 1: Download WordPress

cd /var/www/apps
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo chown -R www-data:www-data wordpress

Step 2: Create Database

mysql -u root -p
CREATE DATABASE wordpress_db;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;

Step 3: WordPress Configuration

cd /var/www/apps/wordpress
cp wp-config-sample.php wp-config.php
nano wp-config.php
define('DB_NAME', 'wordpress_db');
define('DB_USER', 'wordpress_user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

Step 4: Nginx Configuration

sudo nano /etc/nginx/sites-available/wordpress
server {
    listen 80;
    server_name your-domain.com;
    root /var/www/apps/wordpress;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

🔴 Laravel Deploy

Step 1: Create Laravel Project

cd /var/www/apps
composer create-project laravel/laravel laravel-app
cd laravel-app

Step 2: Environment Setup

cp .env.example .env
php artisan key:generate

Step 3: Database Configuration

nano .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password

Step 4: Run Migrations

php artisan migrate

Step 5: Nginx Configuration

sudo nano /etc/nginx/sites-available/laravel
server {
    listen 80;
    server_name your-domain.com;
    root /var/www/apps/laravel-app/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Step 6: Set Permissions

sudo chown -R www-data:www-data /var/www/apps/laravel-app
sudo chmod -R 755 /var/www/apps/laravel-app
sudo chmod -R 777 /var/www/apps/laravel-app/storage
sudo chmod -R 777 /var/www/apps/laravel-app/bootstrap/cache

How can we help?