Node.js Uygulaması Deploy Etme: PM2 ve Nginx
Node.js uygulamasını VPS'e deploy etme rehberi. nvm ile Node.js kurulumu, PM2 process manager, Nginx reverse proxy, SSL sertifikası ve environment variables yönetimi.
PHP uygulamasını VPS'e deploy etme rehberi. LAMP ve LEMP stack karşılaştırması, Apache/Nginx + PHP-FPM + MySQL kurulumu, sanal host yapılandırması ve PHP sürüm yönetimi.
PHP, web geliştirmenin temel taşlarından biridir ve dünya genelinde milyonlarca web sitesini güçlendirir. Bu rehberde LAMP (Linux + Apache + MySQL + PHP) ve LEMP (Linux + Nginx + MySQL + PHP) stack'lerini karşılaştıracak, kurulum adımlarını ve PHP uygulamanızı deploy etmeyi ele alacağız.
| Özellik | LAMP | LEMP |
|---|---|---|
| Web Sunucu | Apache | Nginx |
| Performans | Orta-yüksek | Yüksek |
| Bellek Kullanımı | Daha fazla | Daha az |
| .htaccess | Destekler | Desteklemez |
| PHP Entegrasyonu | mod_php veya PHP-FPM | PHP-FPM (zorunlu) |
| Statik Dosya | İyi | Çok iyi |
| Yapılandırma | Daha kolay | Biraz daha karmaşık |
WordPress, Laravel gibi PHP uygulamaları için LEMP stack daha iyi performans sunar. .htaccess gerektiren eski uygulamalar için LAMP tercih edilebilir.
# Paket listesini güncelle
sudo apt update && sudo apt upgrade -y
# Nginx kur
sudo apt install nginx -y
# Servisi başlat ve etkinleştir
sudo systemctl start nginx
sudo systemctl enable nginx
# Durumu kontrol et
sudo systemctl status nginx
# MariaDB kur (MySQL alternatifi, daha hızlı)
sudo apt install mariadb-server mariadb-client -y
# Servisi başlat
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Güvenlik yapılandırması
sudo mysql_secure_installation
Güvenlik yapılandırmasında:
# Veritabanı ve kullanıcı oluştur
sudo mysql -u root -p
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'guclu-sifre';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
# PHP deposunu ekle (Ubuntu için)
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
# PHP 8.2 ve yaygın eklentileri kur
sudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl \
php8.2-mbstring php8.2-zip php8.2-gd php8.2-bcmath php8.2-intl \
php8.2-redis php8.2-imagick -y
# PHP-FPM servisini başlat
sudo systemctl start php8.2-fpm
sudo systemctl enable php8.2-fpm
# PHP sürümünü kontrol et
php --version
# PHP.ini düzenle
sudo nano /etc/php/8.2/fpm/php.ini
Önemli ayarlar:
; Bellek limiti
memory_limit = 256M
; Maksimum yükleme boyutu
upload_max_filesize = 64M
post_max_size = 64M
; Maksimum çalışma süresi
max_execution_time = 300
; Hata gösterimi (üretimde kapalı olmalı)
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log
; OPcache (performans için)
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 2
# PHP-FPM'i yeniden başlat
sudo systemctl restart php8.2-fpm
# Uygulama dizini oluştur
sudo mkdir -p /var/www/myapp/public
sudo chown -R www-data:www-data /var/www/myapp
# Sanal host yapılandırması
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name example.com www.example.com;
root /var/www/myapp/public;
index index.php index.html;
# Laravel/Symfony için URL yeniden yazma
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP-FPM ile PHP dosyalarını işle
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
# Gizli dosyalara erişimi engelle
location ~ /\.(?!well-known).* {
deny all;
}
# Statik dosyalar için cache
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Log dosyaları
access_log /var/log/nginx/myapp.access.log;
error_log /var/log/nginx/myapp.error.log;
# Dosya yükleme boyutu
client_max_body_size 64M;
}
# Siteyi etkinleştir
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
# Apache kur
sudo apt install apache2 -y
# Gerekli modülleri etkinleştir
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
# Apache sanal host
sudo nano /etc/apache2/sites-available/myapp.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/myapp/public
<Directory /var/www/myapp/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# PHP-FPM ile
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined
</VirtualHost>
# Siteyi etkinleştir
sudo a2ensite myapp.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
Birden fazla PHP sürümü aynı sunucuda çalışabilir:
# Birden fazla PHP sürümü kur
sudo apt install php7.4 php7.4-fpm php8.1 php8.1-fpm php8.2 php8.2-fpm -y
# CLI için varsayılan PHP sürümünü değiştir
sudo update-alternatives --config php
# Nginx'te farklı site için farklı PHP sürümü
# php8.2-fpm.sock yerine php7.4-fpm.sock kullan
# Repoyu klonla
cd /var/www
git clone https://github.com/kullanici/myapp.git
cd myapp
# Composer bağımlılıklarını kur
composer install --no-dev --optimize-autoloader
# .env dosyasını oluştur
cp .env.example .env
nano .env
# Uygulama anahtarı oluştur
php artisan key:generate
# Veritabanı migration
php artisan migrate --force
# Cache oluştur
php artisan config:cache
php artisan route:cache
php artisan view:cache
# Dosya izinlerini ayarla
sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 755 /var/www/myapp
sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache
# Certbot kur
sudo apt install certbot python3-certbot-nginx -y
# SSL sertifikası al
sudo certbot --nginx -d example.com -d www.example.com
# Otomatik yenilemeyi test et
sudo certbot renew --dry-run
REXE sunucularında Path Panel üzerinden 80 ve 443 portlarının açık olduğundan emin olun. MySQL için 3306 portunu dışarıya kapalı tutun.
PHP uygulamanız artık LEMP veya LAMP stack üzerinde çalışıyor. PHP-FPM ile Nginx kombinasyonu yüksek performans sunarken, OPcache ile PHP kodunuz önbelleğe alınarak çok daha hızlı çalışır. Düzenli güvenlik güncellemeleri ve PHP sürüm yönetimi ile uygulamanızı güncel tutun.
Yeni projeler için LEMP (Nginx + PHP-FPM) önerilir; daha az bellek kullanır ve daha yüksek performans sunar. Ancak .htaccess kullanan eski PHP uygulamaları veya WordPress için LAMP (Apache) daha kolay yapılandırılır. Her iki stack de üretim ortamı için uygundur.
PHP-FPM (FastCGI Process Manager), PHP'yi ayrı bir process olarak çalıştıran ve web sunucusundan bağımsız yöneten bir araçtır. mod_php'ye göre daha az bellek kullanır, daha iyi performans sunar ve farklı siteler için farklı PHP sürümleri kullanmayı mümkün kılar.
Ondrej PPA deposu ile birden fazla PHP sürümü kurulabilir. Her sürüm için ayrı PHP-FPM servisi çalışır (php7.4-fpm, php8.1-fpm, php8.2-fpm). Nginx'te her site için farklı PHP-FPM socket kullanarak farklı PHP sürümleri atanabilir.
OPcache'i etkinleştirin (opcache.enable=1), PHP-FPM worker sayısını artırın, Nginx'te statik dosyalar için cache header ekleyin. Laravel/Symfony için 'php artisan config:cache' ve 'php artisan route:cache' komutlarını çalıştırın. Redis ile session ve cache yönetimini optimize edin.
Node.js uygulamasını VPS'e deploy etme rehberi. nvm ile Node.js kurulumu, PM2 process manager, Nginx reverse proxy, SSL sertifikası ve environment variables yönetimi.
VPS'te WordPress kurulumu ve optimizasyonu rehberi. LEMP stack, wp-config.php ayarları, dosya izinleri, SSL, OPcache performans optimizasyonu ve güvenlik sertleştirme.
Python/Django uygulamasını VPS'e deploy etme rehberi. Virtualenv kurulumu, Django üretim ayarları, Gunicorn WSGI sunucusu, Nginx reverse proxy, systemd servisi ve statik dosya yönetimi.