---
# System prepended metadata

title: '[Nginx] 安裝'
tags: [Nginx, server]

---

# [Nginx] 安裝
###### tags: `server` `web server` `Nginx`

### :triangular_flag_on_post:
### 1. 先裝ufw 並把ufw enable
```=linux
    sudo apt-get install ufw
    sudo ufw allow 'Nginx Full'
    sudo ufw status
    sudo ufw enable
    sudo systemctl enable nginx
```
### 2. 建立網站路徑
```=linux
    sudo mkdir -p /var/www/example.com/html
    sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chmod -R 755 /var/www/example.com
```
### 3. 建立Nginx設定檔
```=linux
    sudo vim /etc/nginx/sites-available/example.com
```
```
    server {
            listen 80;
            listen [::]:80;

            root /var/www/example.com/html;
            index index.html index.htm index.nginx-debian.html;

            server_name example.com www.example.com;

            location / {
                    try_files $uri $uri/ =404;
            }
    }    
```

### 4.增加 soft link
```=linux
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

sudo nginx -t
sudo systemctl restart nginx
```

若須建立多個網站重覆步驟2-4
最後設定domain dns

