Panduan Pemasangan Nginx
Keperluan Persekitaran
Nginx menyokong pelbagai sistem pengendalian termasuk:
- Linux (Debian, Ubuntu, CentOS, RHEL, Alpine, dll.)
- FreeBSD
- Solaris
- AIX
- HP-UX
- macOS
- Windows
Kaedah Pemasangan
Kaedah 1: Menggunakan Pengurus Pakej (Disyorkan)
Ubuntu/Debian
# Update package index
sudo apt update
# Install Nginx
sudo apt install nginx
# Start Nginx
sudo systemctl start nginx
# Enable on boot
sudo systemctl enable nginx
CentOS/RHEL
# Install EPEL repository
sudo yum install epel-release
# Install Nginx
sudo yum install nginx
# Start Nginx
sudo systemctl start nginx
# Enable on boot
sudo systemctl enable nginx
Alpine
apk add nginx
macOS
# Install using Homebrew
brew install nginx
# Start Nginx
brew services start nginx
Kaedah 2: Memasang dari Sumber
Pasang Kebergantungan Binaan
Debian/Ubuntu:
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev
CentOS/RHEL:
sudo yum install gcc pcre-devel zlib-devel openssl-devel
Muat Turun Kod Sumber
# Download latest stable version
wget https://nginx.org/download/nginx-1.26.0.tar.gz
# Extract
tar -zxf nginx-1.26.0.tar.gz
# Enter directory
cd nginx-1.26.0
Konfigurasi Binaan
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_stub_status_module \
--with-pcre
Bina dan Pasang
# Compile
make
# Install
sudo make install
Kaedah 3: Menggunakan Docker
# Pull official image
docker pull nginx
# Run Nginx container
docker run -d -p 80:80 --name mynginx nginx
# Mount configuration and logs
docker run -d -p 80:80 \
-v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /host/logs:/var/log/nginx \
--name mynginx nginx
Kaedah 4: Pemasangan Windows
- Muat turun versi Windows dari https://nginx.org/en/download.html
- Ekstrak ke direktori yang dikehendaki
- Klik dua kali nginx.exe untuk memulakan
- Atau mulakan dari baris arahan:
cd C:\nginx
start nginx
Pengesahan Selepas Pemasangan
Semak Pemasangan
# Check Nginx version
nginx -v
# View detailed version information
nginx -V
Semak Status Berjalan
# Check if process is running
ps aux | grep nginx
# Check status using systemctl
systemctl status nginx
Uji Akses
# Use curl to test
curl http://localhost
# Or open browser to access
http://localhost
Struktur Direktori
Direktori Lalai Linux
| Laluan | Penerangan |
|---|---|
| /etc/nginx/ | Direktori konfigurasi |
| /etc/nginx/nginx.conf | Fail konfigurasi utama |
| /etc/nginx/conf.d/ | Direktori sub-konfigurasi |
| /var/log/nginx/ | Direktori log |
| /var/www/html/ | Akar dokumen lalai |
| /usr/sbin/nginx | Fail boleh laksana |
| /usr/share/nginx/html/ | Direktori fail statik lalai |
Direktori Pemasangan Terkompil
| Laluan | Penerangan |
|---|---|
| /usr/local/nginx/ | Direktori akar pemasangan |
| /usr/local/nginx/conf/ | Direktori konfigurasi |
| /usr/local/nginx/html/ | Akar dokumen lalai |
| /usr/local/nginx/logs/ | Direktori log |
| /usr/local/nginx/sbin/ | Direktori boleh laksana |
Menyahpasang Nginx
Debian/Ubuntu
sudo apt remove nginx nginx-common nginx-full
CentOS/RHEL
sudo yum remove nginx
Pemasangan Terkompil
# Stop Nginx
sudo nginx -s stop
# Remove installation directory
sudo rm -rf /usr/local/nginx
Isu Lazim
Konflik Port
Jika permulaan gagal disebabkan konflik port:
# Check port usage
netstat -tulpn | grep :80
# Modify Nginx listening port
# Edit /etc/nginx/nginx.conf
# Change listen 80; to listen 8080;
Masalah Kebenaran
Jika anda menghadapi ralat kebenaran:
# Check Nginx user
ps aux | grep nginx
# Fix file permissions
sudo chown -R www-data:www-data /var/www/html/
Semakan Sintaks Konfigurasi
# Test configuration syntax
nginx -t
# Test with specific configuration file
nginx -t -c /path/to/nginx.conf
