Panduan Instalasi Nginx
Persyaratan Lingkungan
Nginx mendukung beberapa sistem operasi termasuk:
- Linux (Debian, Ubuntu, CentOS, RHEL, Alpine, dll.)
- FreeBSD
- Solaris
- AIX
- HP-UX
- macOS
- Windows
Metode Instalasi
Metode 1: Menggunakan Manajer Paket (Disarankan)
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
Metode 2: Menginstal dari Sumber
Instal Dependensi Build
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
Unduh Kode 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
Konfigurasikan Build
./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
Build dan Instal
# Compile
make
# Install
sudo make install
Metode 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
Metode 4: Instalasi Windows
- Unduh versi Windows dari https://nginx.org/en/download.html
- Ekstrak ke direktori yang diinginkan
- Klik dua kali nginx.exe untuk memulai
- Atau mulai dari baris perintah:
cd C:\nginx
start nginx
Verifikasi Pasca-Instalasi
Periksa Instalasi
# Check Nginx version
nginx -v
# View detailed version information
nginx -V
Periksa 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 Default Linux
| Path | Deskripsi |
|---|---|
| /etc/nginx/ | Direktori konfigurasi |
| /etc/nginx/nginx.conf | File konfigurasi utama |
| /etc/nginx/conf.d/ | Direktori sub-konfigurasi |
| /var/log/nginx/ | Direktori log |
| /var/www/html/ | Root dokumen default |
| /usr/sbin/nginx | File executable |
| /usr/share/nginx/html/ | Direktori file statis default |
Direktori Instalasi Terkompilasi
| Path | Deskripsi |
|---|---|
| /usr/local/nginx/ | Direktori root instalasi |
| /usr/local/nginx/conf/ | Direktori konfigurasi |
| /usr/local/nginx/html/ | Root dokumen default |
| /usr/local/nginx/logs/ | Direktori log |
| /usr/local/nginx/sbin/ | Direktori executable |
Menghapus Instalasi Nginx
Debian/Ubuntu
sudo apt remove nginx nginx-common nginx-full
CentOS/RHEL
sudo yum remove nginx
Instalasi Terkompilasi
# Stop Nginx
sudo nginx -s stop
# Remove installation directory
sudo rm -rf /usr/local/nginx
Masalah Umum
Konflik Port
Jika startup gagal karena 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 Izin
Jika Anda menemui kesalahan izin:
# Check Nginx user
ps aux | grep nginx
# Fix file permissions
sudo chown -R www-data:www-data /var/www/html/
Pemeriksaan Sintaks Konfigurasi
# Test configuration syntax
nginx -t
# Test with specific configuration file
nginx -t -c /path/to/nginx.conf
