So installierst du das Laravel PHP Framework mit Nginx und kostenlosem Let’s Encrypt SSL auf AlmaLinux 8

Laravel ist ein kostenloses, quelloffenes und leichtgewichtiges PHP-Webframework, das für die Entwicklung PHP-basierter Webanwendungen verwendet wird. Es ist beliebt wegen seiner eleganten Syntax, fortschrittlichen Funktionen und robusten Tools. Es basiert auf dem Symfony-Framework und hilft Entwicklern, die Entwicklung von Webanwendungen zu vereinfachen. Außerdem bietet es eine Artisan-Befehlszeilenschnittstelle, mit der du die Operationen für deine Anwendungen durchführen kannst. Es bietet leistungsstarke Funktionen wie Artisan, MVC-Architektur, objektrelationales Mapping, Template Engine, Unit-Testing und Datenbankmigrationssystem.

In diesem Beitrag zeigen wir dir, wie du Laravel mit Nginx auf Alma Linux 8 installierst.

Voraussetzungen

  • Ein Server, auf dem Alma Linux 8 läuft.
  • Ein gültiger Domainname, der auf die IP deines Servers zeigt.
  • Ein Root-Passwort ist auf deinem Server eingerichtet.

LEMP-Server installieren

Zuerst musst du Nginx, MariaDB, PHP und andere erforderliche PHP-Erweiterungen auf deinem Server installieren. Du kannst alle Pakete mit folgendem Befehl installieren:

dnf install nginx mariadb-server php php-fpm php-common php-xml php-mbstring php-json php-zip php-mysqlnd curl unzip -y

Nachdem du alle Pakete installiert hast, bearbeite die Konfigurationsdatei php-fpm und konfiguriere sie so, dass sie Nginx verwendet:

nano /etc/php-fpm.d/www.conf

Ändere die folgenden Zeilen:

listen.owner = nginx
listen.group = nginx

Speichere und schließe die Datei, bearbeite dann die PHP-Konfigurationsdatei und ändere die Standardwerte:

nano /etc/php.ini

Ändere die folgenden Zeilen:

date.timezone = Asia/Kolkata
cgi.fix_pathinfo=1

Speichere und schließe die Datei. Starte und aktiviere dann den Nginx-, MariaDB- und PHP-FPM-Dienst mit dem folgenden Befehl:

systemctl start nginx
systemctl start mariadb
systemctl start php-fpm
systemctl enable nginx
systemctl enable mariadb
systemctl enable php-fpm

Wenn du damit fertig bist, kannst du mit dem nächsten Schritt fortfahren.

Composer installieren

In diesem Beitrag werden wir Laravel mit Hilfe des Composers installieren. Daher musst du den Composer auf deinem System installieren. Du kannst ihn installieren, indem du den folgenden Befehl ausführst:

curl -sS https://getcomposer.org/installer | php

Du erhältst die folgende Ausgabe:

All settings correct for using Composer
Downloading...

Composer (version 2.2.3) successfully installed to: /root/composer.phar
Use it: php composer.phar

Als Nächstes verschiebst du die Composer-Binärdatei in den Systempfad und gibst mit dem folgenden Befehl die entsprechenden Berechtigungen ein:

mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Als Nächstes überprüfst du die Composer-Version mit dem folgenden Befehl:

composer --version

Du erhältst die folgende Ausgabe:

Composer version 2.2.3 2021-12-31 12:18:53

Laravel auf Alma Linux 8 installieren

Wechsle als Nächstes in das Nginx-Web-Root-Verzeichnis und installiere Laravel mit Hilfe des Composers:

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

Du erhältst die folgende Ausgabe:

Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
69 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate --ansi
Application key set successfully.

Als Nächstes musst du Laravel die richtigen Eigentumsrechte und Berechtigungen zuweisen:

chown -R nginx:nginx /var/www/html/laravel/
chown -R nginx:nginx /var/www/html/laravel/storage/
chown -R nginx:nginx /var/www/html/laravel/bootstrap/cache/
chmod -R 0777 /var/www/html/laravel/storage/
chmod -R 0775 /var/www/html/laravel/bootstrap/cache/

Wenn du damit fertig bist, kannst du mit dem nächsten Schritt fortfahren.

Einen virtuellen Nginx-Host für Laravel erstellen

Als Nächstes musst du eine Nginx-Konfigurationsdatei für Laravel erstellen. Du kannst sie mit dem folgenden Befehl erstellen:

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

Füge die folgenden Zeilen ein:

server {
       listen 80;
       server_name laravel.exampledomain.com;
       root        /var/www/html/laravel/public;
       index       index.php;
       charset utf-8;
       gzip on;
	gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
        location / {
        	try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php {
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/www.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

Speichere und schließe die Datei und überprüfe Laravel auf Konfigurationsfehler:

nginx -t

Du solltest die folgende Ausgabe erhalten:

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

Starte als Nächstes den Nginx- und PHP-FPM-Dienst neu, um die Änderungen zu übernehmen:

systemctl restart php-fpm
systemctl restart nginx

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

systemctl status nginx

Du erhältst die folgende Ausgabe:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           ??php-fpm.conf
   Active: active (running) since Fri 2022-01-07 08:29:11 UTC; 4s ago
  Process: 8186 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 8184 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 8182 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 8188 (nginx)
    Tasks: 2 (limit: 11411)
   Memory: 3.7M
   CGroup: /system.slice/nginx.service
           ??8188 nginx: master process /usr/sbin/nginx
           ??8189 nginx: worker process

Jan 07 08:29:11 linux systemd[1]: nginx.service: Succeeded.
Jan 07 08:29:11 linux systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Jan 07 08:29:11 linux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 07 08:29:11 linux nginx[8184]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 07 08:29:11 linux nginx[8184]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 07 08:29:11 linux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 07 08:29:11 linux systemd[1]: Started The nginx HTTP and reverse proxy server.

Firewall für Laravel konfigurieren

Als nächstes musst du die Ports 80 und 443 in der Firewall zulassen. Du kannst sie mit dem folgenden Befehl zulassen:

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https

Lade anschließend die Firewalld neu, um die Änderungen zu übernehmen:

firewall-cmd --reload

Zugriff auf Laravel Web UI

Öffne nun deinen Webbrowser und rufe die Laravel Web UI über die URL http://laravel.exampledomain.com auf. Du solltest die Laravel-Standardseite auf dem folgenden Bildschirm sehen:

Laravel

Aktiviere SSL auf der Laravel-Website

Es wird empfohlen, SSL auf der Laravel-Website zu aktivieren, um die Verbindung zu sichern. Let’s Encrypt bietet ein kostenloses SSL an, mit dem du SSL/TLS-Zertifikate für deine Domain erwerben, erneuern und verwalten kannst. Installiere zunächst den Certbot-Client mit dem folgenden Befehl:

dnf install epel-release -y
dnf install certbot -y

Als Nächstes führst du den folgenden Befehl aus, um Let’s Encrypt SSL für deine Laravel-Domain herunterzuladen:

certbot --nginx -d laravel.exampledomain.com

Du wirst aufgefordert, deine 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.exampledomain.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:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 den Vorgang zu starten. Sobald das Zertifikat installiert ist, solltest du 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.exampledomain.com

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

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/privkey.pem
   Your cert will expire on 2022-04-11. 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.

Zu diesem Zeitpunkt ist deine Laravel-Website mit Let’s Encrypt SSL gesichert. Du kannst sie jetzt sicher über die URL https://laravel.exampledomain.com aufrufen .

Fazit

Herzlichen Glückwunsch! Du hast Laravel mit Nginx und Let’s Encrypt SSL erfolgreich auf Alma Linux 8 installiert und kannst nun mit der Entwicklung PHP-basierter Anwendungen mit dem Laravel-Framework beginnen. Wenn du noch Fragen hast, kannst du dich gerne an mich wenden.

Das könnte dich auch interessieren …