# Huong dan cai tren server moi

Tai lieu nay dung cho Ubuntu/Debian co Apache2 va chay source nay bang `mod_wsgi`.

## 1. Chuan bi source

Copy toan bo project len server, vi du:

```bash
sudo mkdir -p /opt/misa-user
sudo rsync -av --delete "/duong-dan-source/" /opt/misa-user/
cd /opt/misa-user
```

Neu ban copy bang SCP hoac WinSCP thi chi can dam bao thu muc dich chua day du:

- `app.py`
- `app.wsgi`
- `templates/`
- `ssl/fullchain.pem`
- `ssl/key.pem`
- `apache2/misa-user.conf`
- `setup_apache2.sh`

## 2. Cai Apache2 va package can thiet

Chay nhanh bang script da co san:

```bash
cd /opt/misa-user
chmod +x setup_apache2.sh
sudo ./setup_apache2.sh
```

Script nay se:

- cai `apache2`, `libapache2-mod-wsgi-py3`, `python3-pip`, `unixodbc`
- cai Python package: `flask`, `pandas`, `openpyxl`, `filelock`, `pyodbc`
- tao symlink `/var/www/misa-user-app` tro den source that
- copy file site Apache vao `/etc/apache2/sites-available/misa-user.conf`
- bat `ssl`, `rewrite`, `headers`, `wsgi`
- kiem tra config bang `apache2ctl -t`
- reload Apache2

## 3. Cai ODBC Driver cho SQL Server

Ung dung dung `pyodbc`, vi vay Linux server can co SQL Server ODBC driver. `db.py` da tu uu tien:

- `ODBC Driver 18 for SQL Server`
- `ODBC Driver 17 for SQL Server`
- `SQL Server`

Kiem tra driver dang co:

```bash
odbcinst -q -d
```

Neu chua co `ODBC Driver 18 for SQL Server`, cai them:

```bash
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc >/dev/null
curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt update
sudo ACCEPT_EULA=Y apt install -y msodbcsql18
```

Neu server khong phai Ubuntu 22.04, doi URL repo Microsoft cho dung version OS.

## 4. Khai bao bien moi truong cho database

Mac dinh hien tai trong code:

- `SQL_SERVER=kdkt.ddns.net`
- `SQL_USER=hotay`
- `SQL_PASSWORD=ZBIsQaoNHBh8jyJvSJMq`

Nen override bang file environment rieng cua Apache.

Tao file:

```bash
sudo nano /etc/apache2/envvars.d/misa-user
```

Noi dung de xuat:

```bash
export SQL_SERVER="ten-sql-server"
export SQL_USER="sql-user"
export SQL_PASSWORD="sql-password"
export SQL_DRIVER="{ODBC Driver 18 for SQL Server}"
```

Sau do nap file nay trong Apache envvars:

```bash
echo '. /etc/apache2/envvars.d/misa-user' | sudo tee -a /etc/apache2/envvars
sudo systemctl restart apache2
```

Neu khong muon sua `/etc/apache2/envvars`, ban co the them truc tiep cac dong `SetEnv` vao VirtualHost trong `misa-user.conf`.

## 5. Gan domain hoac IP

File mau hien tai la `apache2/misa-user.conf` va dang de:

```apache
ServerName localhost
```

Tren server that, sua lai thanh domain hoac IP cua may:

```apache
<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    SSLEngine on
    SSLCertificateFile /var/www/misa-user-app/ssl/fullchain.pem
    SSLCertificateKeyFile /var/www/misa-user-app/ssl/key.pem
```

Neu chay bang IP noi bo, co the tam thoi de `ServerName 192.168.x.x`.

## 6. Chung chi SSL

Source dang tro vao:

- `/var/www/misa-user-app/ssl/fullchain.pem`
- `/var/www/misa-user-app/ssl/key.pem`

Can dam bao 2 file nay ton tai va Apache doc duoc:

```bash
sudo ls -l /var/www/misa-user-app/ssl/
```

Neu dung cert khac, sua lai duong dan trong `/etc/apache2/sites-available/misa-user.conf`.

## 7. Kiem tra sau khi cai

Kiem tra syntax Apache:

```bash
sudo apache2ctl -t
```

Kiem tra module va site da bat:

```bash
sudo a2query -m wsgi
sudo a2query -m ssl
sudo a2query -s misa-user
```

Restart dich vu:

```bash
sudo systemctl restart apache2
sudo systemctl status apache2 --no-pager
```

Thu truy cap:

```bash
curl -k https://127.0.0.1/
```

## 8. Log can xem khi loi

Log Apache cua app:

```bash
sudo tail -f /var/log/apache2/misa-user-error.log
```

Log he thong Apache:

```bash
sudo journalctl -u apache2 -n 100 --no-pager
```

## 9. Loi thuong gap

### Loi `Can't open lib 'ODBC Driver 18 for SQL Server'`

Server chua cai `msodbcsql18` hoac dang dung ten driver khac. Chay:

```bash
odbcinst -q -d
```

roi cap nhat `SQL_DRIVER` cho dung.

### Loi `ImportError: No module named ...`

Chua cai Python package tren server. Chay lai:

```bash
cd /opt/misa-user
sudo ./setup_apache2.sh
```

hoac cai thu cong bang `pip`.

### Loi SSL khong len

Kiem tra lai:

- file cert co ton tai khong
- file key co dung cap voi cert khong
- Apache da bat module `ssl` chua

### Loi 500 nhung Apache van chay

Kha nang cao la app van vao duoc WSGI nhung loi:

- ket noi SQL Server
- doc file Excel `phan_quyen.xlsx`
- thieu quyen doc file trong thu muc source

Kiem tra log o muc 8.

## 10. Duong dan quan trong

- Source that: `/opt/misa-user`
- Symlink Apache: `/var/www/misa-user-app`
- Site config: `/etc/apache2/sites-available/misa-user.conf`
- WSGI entrypoint: `/var/www/misa-user-app/app.wsgi`
- SSL files: `/var/www/misa-user-app/ssl/fullchain.pem` va `/var/www/misa-user-app/ssl/key.pem`

## 11. Lenh tong hop de dung nhanh

```bash
sudo mkdir -p /opt/misa-user
sudo rsync -av --delete "/duong-dan-source/" /opt/misa-user/
cd /opt/misa-user
chmod +x setup_apache2.sh
sudo ./setup_apache2.sh
sudo apache2ctl -t
sudo systemctl restart apache2
```

Neu can sua domain, cert, hoac bien moi truong DB thi sua xong roi moi restart Apache.