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.
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
| Merkmal | LAMP | LEMP |
|---|---|---|
| Webserver | Apache | Nginx |
| Performance | Mittel-hoch | Hoch |
| Speicherverbrauch | Mehr | Weniger |
| .htaccess | Unterstützt | Nicht unterstützt |
| PHP-Integration | mod_php oder PHP-FPM | PHP-FPM (erforderlich) |
| Statische Dateien | Gut | Sehr 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
# 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
# MariaDB installieren
sudo apt install mariadb-server mariadb-client -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Sicherheitskonfiguration
sudo mysql_secure_installation
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
# 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
; 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)
sudo mkdir -p /var/www/myapp/public
sudo chown -R www-data:www-data /var/www/myapp
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;
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;
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Anwendung deployen (Laravel Beispiel)
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
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.
Verwandte Artikel
Node.js App deployen: PM2 und Nginx Anleitung
Vollständige Anleitung zum Deployen von Node.js-Anwendungen auf einem VPS. Node.js-Installation mit nvm, PM2 Process Manager, Nginx Reverse Proxy, SSL-Zertifikat und Umgebungsvariablen.
WordPress Installation und Optimierung: VPS Anleitung
Anleitung zur Installation und Optimierung von WordPress auf einem VPS. LEMP Stack Setup, wp-config.php Einstellungen, Dateiberechtigungen, SSL, OPcache Performance-Optimierung und Sicherheitshärtung.
Python/Django Deploy: Produktion mit Gunicorn und Nginx
Anleitung zum Deployen von Python/Django-Anwendungen auf einem VPS. Virtualenv-Setup, Django-Produktionseinstellungen, Gunicorn WSGI-Server, Nginx Reverse Proxy, systemd-Dienst und statische Dateiverwaltung.