Ana içeriğe geç
Kategoriye Dön

PHP Uygulaması Deploy Etme: LAMP ve LEMP Stack

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.

Okuma süresi: 18 dk Web Hosting / Uygulama
phplamplempapachenginxmysqlphp-fpmdeploylinux

İçindekiler

PHP Uygulaması Deploy Etme: LAMP ve LEMP Stack Rehberi

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.

LAMP vs LEMP Karşılaştırması

ÖzellikLAMPLEMP
Web SunucuApacheNginx
PerformansOrta-yüksekYüksek
Bellek KullanımıDaha fazlaDaha az
.htaccessDesteklerDesteklemez
PHP Entegrasyonumod_php veya PHP-FPMPHP-FPM (zorunlu)
Statik DosyaİyiÇok iyi
YapılandırmaDaha kolayBiraz 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.

LEMP Stack Kurulumu

1. Nginx Kurulumu

hljs bash
# 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

2. MySQL/MariaDB Kurulumu

hljs bash
# 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:

  • Root şifresi belirleyin
  • Anonim kullanıcıları kaldırın
  • Root uzaktan girişi devre dışı bırakın
  • Test veritabanını kaldırın
hljs bash
# Veritabanı ve kullanıcı oluştur
sudo mysql -u root -p
hljs sql
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;

3. PHP ve PHP-FPM Kurulumu

hljs bash
# 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

4. PHP Yapılandırması

hljs bash
# PHP.ini düzenle
sudo nano /etc/php/8.2/fpm/php.ini

Önemli ayarlar:

hljs ini
; 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
hljs bash
# PHP-FPM'i yeniden başlat
sudo systemctl restart php8.2-fpm

Nginx Sanal Host Yapılandırması (PHP için)

hljs bash
# 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
hljs nginx
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;
}
hljs bash
# Siteyi etkinleştir
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

LAMP Stack Kurulumu (Apache ile)

hljs bash
# 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
hljs apache
<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>
hljs bash
# Siteyi etkinleştir
sudo a2ensite myapp.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

PHP Sürüm Yönetimi

Birden fazla PHP sürümü aynı sunucuda çalışabilir:

hljs bash
# 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

Uygulamayı Deploy Etme

Laravel Örneği

hljs bash
# 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

SSL Sertifikası

hljs bash
# 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.

Sonuç

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.