364 lines
No EOL
9.3 KiB
Markdown
364 lines
No EOL
9.3 KiB
Markdown
nginx
|
||
[nginx.md](../../../_resources/nginx.md)
|
||
|
||
- to start nginx server with python use
|
||
`$ python3 -m http.server 8085`
|
||
for a specific directory use
|
||
`python3 -m http.server -d /path/to/your/directory [PORT_NUMBER]`
|
||
|
||
## Tip
|
||
- Instead of buying or getting actual domain, go to `/etc/hosts` and add the line `127.0.0.1 <domain_name>`. so when u type this added domain name in browser it will redirect to the ip address automatically
|
||
Eg:
|
||
```yaml
|
||
server {
|
||
listen 80;
|
||
|
||
server_name skdnbdasbn.org;
|
||
|
||
|
||
location / {
|
||
|
||
root /var/www/testFlex;
|
||
|
||
index index.html index.htm;
|
||
}
|
||
}
|
||
|
||
|
||
server {
|
||
listen 80;
|
||
server_name klsjdisabj.org;
|
||
|
||
location / {
|
||
|
||
root /var/www/test2;
|
||
|
||
index index.html index.htm;
|
||
}
|
||
}
|
||
|
||
|
||
```
|
||
|
||
|
||
To host your `index.html` file using Nginx on Arch Linux, you'll need to go through a few straightforward steps: installation, configuration, and testing. Here's a beginner-friendly guide to get you started:
|
||
|
||
### 1. Installing Nginx
|
||
|
||
First, you need to install Nginx. Arch Linux uses pacman as its package manager, so you can easily install Nginx from the terminal. Open your terminal and run the following command:
|
||
|
||
```bash
|
||
sudo pacman -Sy nginx
|
||
```
|
||
|
||
This command will update your package database and install Nginx.
|
||
|
||
### 2. Starting and Enabling Nginx Service
|
||
|
||
After installing, you need to start and enable the Nginx service to ensure it runs on boot. Use systemctl to manage the Nginx service:
|
||
|
||
```bash
|
||
sudo systemctl start nginx
|
||
sudo systemctl enable nginx
|
||
```
|
||
|
||
### 3. Configuring Nginx
|
||
|
||
Once Nginx is installed, you'll need to configure it to serve your `index.html`. Nginx configurations are stored in `/etc/nginx/`. The primary configuration file is `/etc/nginx/nginx.conf`, but for site-specific configurations, we'll use the `/etc/nginx/sites-available/` and `/etc/nginx/sites-enabled/` directories, which you might need to create. Arch Linux's default setup usually doesn't include these, and instead, you can place your configuration in `/etc/nginx/nginx.conf` or create a specific `server` block file within `/etc/nginx/conf.d/`.
|
||
|
||
For a cleaner setup, let’s create `sites-available` and `sites-enabled` directories and include them in our Nginx configuration:
|
||
|
||
```bash
|
||
sudo mkdir /etc/nginx/sites-available
|
||
sudo mkdir /etc/nginx/sites-enabled
|
||
```
|
||
|
||
Then, open `/etc/nginx/nginx.conf` with your preferred text editor (like nano or vim):
|
||
|
||
```bash
|
||
sudo nano /etc/nginx/nginx.conf
|
||
```
|
||
|
||
Find the `http` block and add the following line at the bottom of it to include any configurations you'll put in `sites-enabled`:
|
||
|
||
```nginx
|
||
include /etc/nginx/sites-enabled/*;
|
||
```
|
||
|
||
Now, create a configuration file for your site in the `sites-available` directory. You can name it after your domain or project
|
||
|
||
4. **Create a new configuration file** for your site in the `sites-available` directory. Replace `yourdomain.com` with your domain name or `localhost` if you don’t have one:
|
||
```
|
||
sudo nano sites-available/yourdomain.com
|
||
```
|
||
|
||
5. Add the following server block (adjust the `server_name` if using a domain, and change `/path/to/your/index.html` to the actual path of your `index.html` file):
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name yourdomain.com; # Or localhost
|
||
location / {
|
||
root /path/to/your;
|
||
index index.html;
|
||
}
|
||
}
|
||
```
|
||
Save and close the file.
|
||
|
||
6. **Create a symbolic link** to enable the site:
|
||
```
|
||
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
|
||
```
|
||
|
||
and finally start and enable nginx service
|
||
|
||
- multiple sites in different domain
|
||
|
||
`sites/available/first`
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name yourdomain1.com;
|
||
location / {
|
||
root /path/to/first;
|
||
index index.html;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
```
|
||
`sites/available/first`
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name yourdomain2.com;
|
||
location / {
|
||
root /path/to/second;
|
||
index index.html;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
```
|
||
|
||
|
||
|
||
|
||
|
||
- For multiple subdomains for one domain and hosting different sites
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name circux.net;
|
||
|
||
# Main configuration for the circux.net domain
|
||
# You can put your main site configuration here
|
||
# ...
|
||
|
||
# Additional server blocks for subdomains
|
||
server {
|
||
listen 80;
|
||
server_name play.circux.net;
|
||
|
||
# Configuration for the play.circux.net subdomain
|
||
# You can put specific settings for this subdomain here
|
||
# ...
|
||
}
|
||
|
||
server {
|
||
listen 80;
|
||
server_name animal.circux.net;
|
||
|
||
# Configuration for the animal.circux.net subdomain
|
||
# You can put specific settings for this subdomain here
|
||
# ...
|
||
}
|
||
|
||
# Add more server blocks for additional subdomains as needed
|
||
# ...
|
||
}
|
||
```
|
||
|
||
- others
|
||
Test Nginx Configuration:
|
||
|
||
Check if there are any syntax errors in your Nginx configuration:
|
||
|
||
`sudo nginx -t`
|
||
|
||
Reload Nginx:
|
||
|
||
Once the configuration test passes, reload Nginx to apply the changes:
|
||
|
||
`sudo systemctl reload nginx`
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### Nodejs server
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name yourdomain.com;
|
||
|
||
location / {
|
||
proxy_pass http://localhost:3000; # Change the port if your Node.js app listens on a different port
|
||
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;
|
||
}
|
||
|
||
# Additional configuration can be added here
|
||
# such as SSL/TLS settings, caching, etc.
|
||
}
|
||
|
||
```
|
||
|
||
|
||
|
||
|
||
### from llm
|
||
|
||
Great! You're almost there. Now that you have a subdomain set up in **FreeDNS (Afraid.org)**, and a **Node.js app running on a specific TCP port on your DigitalOcean VPS**, here’s how to link it all together so that your subdomain points to your Node.js app.
|
||
|
||
---
|
||
|
||
### 🔧 Step-by-Step Guide
|
||
|
||
---
|
||
|
||
#### ✅ 1. **Ensure Your Node.js App is Running**
|
||
|
||
Let’s say your Node.js app is running on:
|
||
|
||
* **IP:** `your_vps_ip`
|
||
* **Port:** `3000`
|
||
|
||
You can test it by visiting `http://your_vps_ip:3000` in a browser.
|
||
|
||
---
|
||
|
||
#### ✅ 2. **Point Subdomain to Your VPS in FreeDNS**
|
||
|
||
In **FreeDNS (Afraid.org)**:
|
||
|
||
* Go to your **subdomain settings**.
|
||
* Set the **A record** of your subdomain (e.g. `app.yourdomain.com`) to the **public IP** of your DigitalOcean VPS.
|
||
|
||
✔️ **Example:**
|
||
|
||
```
|
||
Type: A
|
||
Subdomain: app.yourdomain.com
|
||
Destination: your_vps_ip
|
||
```
|
||
|
||
DNS changes can take up to a few minutes (sometimes longer) to propagate.
|
||
|
||
---
|
||
|
||
#### ✅ 3. **Use a Reverse Proxy with NGINX (Recommended)**
|
||
|
||
Since you’re serving from a port like `3000`, you don't want users to access it via `http://yourdomain.com:3000`. Instead, use **NGINX** to expose it on port 80 (HTTP) or 443 (HTTPS).
|
||
|
||
---
|
||
|
||
#### 🔄 Install and Configure NGINX
|
||
|
||
**Step 1: Install NGINX**
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install nginx
|
||
```
|
||
|
||
**Step 2: Create a New NGINX Config File**
|
||
|
||
```bash
|
||
sudo nano /etc/nginx/sites-available/app
|
||
```
|
||
|
||
**Example Configuration:**
|
||
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name app.yourdomain.com;
|
||
|
||
location / {
|
||
proxy_pass http://localhost:3000;
|
||
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;
|
||
}
|
||
}
|
||
```
|
||
|
||
**Step 3: Enable the Site**
|
||
|
||
```bash
|
||
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
|
||
sudo nginx -t # test config
|
||
sudo systemctl restart nginx
|
||
```
|
||
|
||
---
|
||
|
||
#### ✅ 4. **(Optional) Add HTTPS with Let’s Encrypt**
|
||
|
||
You can secure your site with HTTPS using **Certbot**:
|
||
|
||
```bash
|
||
sudo apt install certbot python3-certbot-nginx
|
||
sudo certbot --nginx -d app.yourdomain.com
|
||
```
|
||
|
||
This will automatically update your NGINX config to use SSL.
|
||
|
||
to renew certificate
|
||
`sudo certbot renew`
|
||
|
||
---
|
||
|
||
### 🎉 Done!
|
||
|
||
Now, visiting `http://app.yourdomain.com` (or `https://app.yourdomain.com` if you added SSL) will open your Node.js app running on port `3000`.
|
||
|
||
---
|
||
|
||
If you want me to walk you through any part (e.g. setting up NGINX or Certbot), just tell me where you're at and I’ll guide you!
|
||
|
||
|
||
|
||
summary of hosting a site
|
||
|
||
1. run that site/service locally with some port
|
||
2. in nginx just to 80 and map your internal port and also provide domain most porobably a domain with dns record ie subdomain or cname(alias of subdomain). so domain is `example.com` so subdomain is `sbdomain.example.com`. domain is not bound to ip but subdomain is which is also `A` dns record
|
||
3. to add dns record use services like cloudfare provides name server.add dns record and map to ip address and link name server provided by clidfare to domain registry site.
|
||
4. finally its recommened to add ssl certificate to get https which ensures trust
|
||
|
||
|
||
|
||
### Extras
|
||
1. There is limit to file upload set by nginx and it can be changed by
|
||
```sh
|
||
nano /etc/nginx/nginx.conf
|
||
# Add a line in the http, server or location section:
|
||
client_max_body_size 100M;
|
||
|
||
# test syntax
|
||
sudo nginx -t
|
||
|
||
# reload nginx
|
||
sudo nginx -s reload
|
||
```
|
||
reference : https://stackoverflow.com/questions/24306335/413-request-entity-too-large-file-upload-issue |