Wie installiere ich das Laravel PHP Framework mit Nginx auf Ubuntu 22.04

Laravel ist ein freies und quelloffenes PHP-Webframework, das von Taylor Otwell entwickelt wurde. Es basiert auf Symfony und folgt dem Model-View-Controller-Architekturmuster. Es wurde für die Entwicklung von High-End-Webanwendungen entwickelt und verfügt über eine aussagekräftige und anmutige Syntax. Es hat viele integrierte Funktionen, die die Entwicklung von Webanwendungen einfacher und schneller machen. Laravel gewann an Popularität, nachdem die Version 3 veröffentlicht wurde, die praktische Funktionen wie die Artisan-Befehlszeile und die Support-Datenbank enthält und ein Paketierungssystem namens Bundles eingeführt hat.

Diese Anleitung zeigt dir, wie du das Laravel PHP Framework mit dem Nginx Webserver auf Ubuntu 22.04 installierst.

Voraussetzungen

  • Ein Server, auf dem Ubuntu 22.04 läuft.
  • Ein gültiger Domainname, der auf die IP deines Servers zeigt.
  • Ein Root-Passwort ist auf dem Server konfiguriert.

LEMP-Server installieren

Bevor du beginnst, musst du den Nginx-Webserver, das MariaDB-Datenbanksystem, PHP und andere erforderliche Abhängigkeiten auf deinem Server installieren. Du kannst alle Pakete mit folgendem Befehl installieren:

apt install -y nginx mariadb-server php php-fpm php-common php-cli php-gd php-mysqlnd php-curl php-intl php-mbstring php-bcmath php-xml php-zip wget git

Sobald alle Pakete installiert sind, überprüfe die PHP-Version mit folgendem Befehl:

php -v

Du solltest die folgende Ausgabe sehen:

PHP 8.1.2 (cli) (built: Apr  7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

Installiere PHP Composer

Composer ist ein Abhängigkeitsmanager für PHP, mit dem du die PHP-Abhängigkeiten verwalten kannst. Um den Composer zu installieren, musst du zunächst das Paket curl auf deinem Server installieren.

apt install -y curl

Anschließend installierst du den PHP Composer mit dem folgenden Befehl:

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

Sobald der Composer installiert ist, erhältst du die folgende Ausgabe:

All settings correct for using Composer
Downloading...

Composer (version 2.3.5) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer

Als Nächstes überprüfst du die Version des Composers mit dem folgenden Befehl:

composer --version

Du solltest die folgende Ausgabe erhalten:

Composer version 2.3.5 2022-04-13 16:43:00

Laravel auf Ubuntu 22.04 installieren

Navigiere zunächst zum Nginx-Web-Root-Verzeichnis und lade die neueste Version von Laravel mit dem Composer-Befehl herunter:

cd /var/www/html
composer create-project laravel/laravel laravel

Du erhältst die folgende Ausgabe:

55 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: spatie/laravel-ignition
Package manifest generated successfully.
78 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

Als Nächstes wechselst du in das Laravel-Verzeichnis und startest Laravel mit folgendem Befehl:

cd laravel
php artisan serve --host 0.0.0.0 --port 8000

Wenn alles in Ordnung ist, solltest du die folgende Ausgabe erhalten:

Starting Laravel development server: http://0.0.0.0:8000
[Sun May 22 08:17:45 2022] PHP 8.1.2 Development Server (http://0.0.0.0:8000) started

Drücke STRG+C, um Laravel zu beenden. Als Nächstes änderst du die Eigentums- und Zugriffsrechte von Laravel:

chown -R www-data:www-data /var/www/html/laravel
chmod -R 0777 /var/www/html/laravel

Nginx für Laravel konfigurieren

Als Nächstes erstellst du mit folgendem Befehl eine Konfigurationsdatei für den virtuellen Nginx-Host für Laravel:

nano /etc/nginx/conf.d/laravel.conf

Füge die folgenden Zeilen hinzu:

server {
    listen 80;
    server_name laravel.example.com;
    root /var/www/html/laravel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Speichere und schließe die Datei, wenn du fertig bist. Überprüfe dann Nginx mit dem folgenden Befehl auf Syntaxfehler:

nginx -t

Du erhältst die folgende Ausgabe:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Starte anschließend den Nginx- und PHP-FPM-Dienst neu, um die Änderungen zu übernehmen:

systemctl restart php8.1-fpm nginx

Du kannst den Status von Nginx auch mit dem folgenden Befehl überprüfen:

systemctl status nginx

Du solltest die folgende Ausgabe sehen:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-05-22 08:19:20 UTC; 17s ago
       Docs: man:nginx(8)
    Process: 16865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 16866 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 16867 (nginx)
      Tasks: 2 (limit: 2292)
     Memory: 2.6M
        CPU: 33ms
     CGroup: /system.slice/nginx.service
             ??16867 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??16868 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

May 22 08:19:20 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 22 08:19:20 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

Zugriff auf das Laravel-Webinterface

Jetzt ist Laravel installiert und mit Nginx konfiguriert. Du kannst nun über die URL http://laravel.example.com auf die Laravel-Web-Oberfläche zugreifen . Auf der folgenden Seite solltest du das Laravel Dashboard sehen:

Laravel mit Let’s Encrypt sichern

Als Nächstes musst du das Certbot Client-Paket installieren, um Let’s Encrypt SSL zu installieren und zu verwalten.

Installiere zunächst Certbot mit dem folgenden Befehl:

apt-get install certbot python3-certbot-nginx -y

Sobald die Installation abgeschlossen ist, führst du den folgenden Befehl aus, um Let’s Encrypt SSL auf deiner Website zu installieren:

certbot --nginx -d laravel.example.com

Du wirst aufgefordert, eine gültige E-Mail-Adresse anzugeben und die Nutzungsbedingungen zu akzeptieren (siehe unten):

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): hitjethva@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for laravel.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/laravel.conf

Als Nächstes wählst du aus, ob der HTTP-Datenverkehr auf HTTPS umgeleitet werden soll oder nicht (siehe unten):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Gib 2 ein und drücke die Eingabetaste, um die Installation abzuschließen. Du solltest die folgende Ausgabe sehen:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/laravel.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://laravel.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=laravel.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/laravel.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/laravel.example.com/privkey.pem
   Your cert will expire on 2022-08-22. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

Schlussfolgerung

Herzlichen Glückwunsch! Du hast Laravel mit Nginx erfolgreich auf Ubuntu 22.04 installiert. Jetzt kannst du damit beginnen, mit dem Laravel-Framework leistungsstarke PHP-Anwendungen zu entwickeln. Wenn du noch Fragen hast, kannst du dich gerne an mich wenden.

Das könnte dich auch interessieren …