Zum Hauptinhalt springen
Zurück zur Kategorie

PHP-App deployen: LAMP und LEMP Stack Anleitung

Vollständige Anleitung zum Deployen von PHP-Anwendungen auf einem VPS. LAMP vs LEMP Vergleich, Apache/Nginx + PHP-FPM + MySQL Setup, Virtual Host Konfiguration und PHP-Versionsverwaltung.

Lesezeit: 18 min Web Hosting / Anwendungen
phplamplempapachenginxmysqlphp-fpmdeploylinux

Inhaltsverzeichnis

PHP-Anwendung deployen: LAMP und LEMP Stack Anleitung

PHP ist einer der Grundpfeiler der Webentwicklung und treibt Millionen von Websites weltweit an. Diese Anleitung vergleicht LAMP (Linux + Apache + MySQL + PHP) und LEMP (Linux + Nginx + MySQL + PHP) Stacks, behandelt Installationsschritte und führt durch das Deployen Ihrer PHP-Anwendung.

LAMP vs LEMP Vergleich

MerkmalLAMPLEMP
WebserverApacheNginx
PerformanceMittel-hochHoch
SpeicherverbrauchMehrWeniger
.htaccessUnterstütztNicht unterstützt
PHP-Integrationmod_php oder PHP-FPMPHP-FPM (erforderlich)
Statische DateienGutSehr gut

LEMP Stack bietet bessere Performance für PHP-Anwendungen wie WordPress und Laravel. LAMP kann für ältere Anwendungen bevorzugt werden, die .htaccess benötigen.

LEMP Stack Installation

1. Nginx installieren

hljs bash
# Paketliste aktualisieren
sudo apt update && sudo apt upgrade -y

# Nginx installieren
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

2. MySQL/MariaDB installieren

hljs bash
# MariaDB installieren
sudo apt install mariadb-server mariadb-client -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

# Sicherheitskonfiguration
sudo mysql_secure_installation
hljs sql
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'starkes-passwort';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

3. PHP und PHP-FPM installieren

hljs bash
# PHP-Repository hinzufügen
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# PHP 8.2 und gängige Erweiterungen installieren
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 -y

sudo systemctl start php8.2-fpm
sudo systemctl enable php8.2-fpm
php --version

4. PHP-Konfiguration

hljs ini
; Speicherlimit
memory_limit = 256M

; Maximale Upload-Größe
upload_max_filesize = 64M
post_max_size = 64M

; Maximale Ausführungszeit
max_execution_time = 300

; Fehleranzeige (in Produktion deaktiviert)
display_errors = Off
log_errors = On

; OPcache (für Performance)
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000

Nginx Virtual Host Konfiguration (für PHP)

hljs bash
sudo mkdir -p /var/www/myapp/public
sudo chown -R www-data:www-data /var/www/myapp
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;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    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;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    client_max_body_size 64M;
}
hljs bash
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Anwendung deployen (Laravel Beispiel)

hljs bash
cd /var/www
git clone https://github.com/benutzer/myapp.git
cd myapp

composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
php artisan migrate --force
php artisan config:cache
php artisan route:cache

sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 775 /var/www/myapp/storage

SSL-Zertifikat

hljs bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run

Stellen Sie auf REXE-Servern sicher, dass die Ports 80 und 443 über das Path Panel geöffnet sind. Halten Sie MySQL-Port 3306 nach außen geschlossen.

Fazit

Ihre PHP-Anwendung läuft jetzt auf einem LEMP- oder LAMP-Stack. Die Kombination Nginx + PHP-FPM liefert hohe Performance, während OPcache Ihren PHP-Code für eine viel schnellere Ausführung zwischenspeichert.