Ricerca nel sito web

Come installare Laravel PHP Framework con Nginx e Free Lets Encrypt SSL su AlmaLinux 8


Su questa pagina

  1. Prerequisiti
  2. Installa il server LEMP
  3. Installa Compositore
  4. Installa Laravel su Alma Linux 8
  5. Crea un host virtuale Nginx per Laravel
  6. Configura il firewall per Laravel
  7. Accedi all'interfaccia utente Web di Laravel
  8. Abilita SSL sul sito web di Laravel
  9. Conclusione

Laravel è un framework web PHP gratuito, open source e leggero utilizzato per creare applicazioni web basate su PHP. È popolare grazie alla sua sintassi elegante, alle funzionalità avanzate e al robusto set di strumenti. Si basa sul framework Symfony e aiuta gli sviluppatori a semplificare lo sviluppo di applicazioni web. Fornisce inoltre un'interfaccia a riga di comando Artisan per eseguire operazioni per le tue applicazioni. Offre potenti funzionalità tra cui Artisan, architettura MVC, mappatura relazionale degli oggetti, motore di modelli, test di unità e sistema di migrazione del database.

In questo post, ti mostreremo come installare Laravel con Nginx su Alma Linux 8.

Prerequisiti

  • Un server che esegue Alma Linux 8.
  • Un nome di dominio valido indicato con l'IP del tuo server.
  • Una password di root è configurata sul tuo server.

Installa il server LEMP

Innanzitutto, dovrai installare Nginx, MariaDB, PHP e altre estensioni PHP richieste sul tuo server. Puoi installarli tutti eseguendo il seguente comando:

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

Dopo aver installato tutti i pacchetti, modifica il file di configurazione php-fpm e configuralo per utilizzare Nginx:

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

Modifica le seguenti righe:

listen.owner = nginx
listen.group = nginx

Salva e chiudi il file, quindi modifica il file di configurazione PHP e modifica i valori predefiniti:

nano /etc/php.ini

Modifica le seguenti righe:

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

Salva e chiudi il file, quindi avvia e abilita il servizio Nginx, MariaDB e PHP-FPM utilizzando il seguente comando:

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

Una volta terminato, puoi procedere al passaggio successivo.

Installa Compositore

In questo post, installeremo Laravel usando il Composer. Quindi dovrai installare Composer sul tuo sistema. Puoi installarlo eseguendo il seguente comando:

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

Otterrai il seguente output:

All settings correct for using Composer
Downloading...

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

Successivamente, sposta il binario Composer nel percorso di sistema e imposta l'autorizzazione corretta con il seguente comando:

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

Successivamente, verifica la versione di Composer con il seguente comando:

composer --version

Otterrai il seguente output:

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

Installa Laravel su Alma Linux 8

Quindi, cambia la directory nella directory root web di Nginx e installa Laravel usando Composer:

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

Otterrai il seguente output:

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.

Successivamente, imposta la proprietà e le autorizzazioni appropriate per Laravel:

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/

Una volta terminato, puoi procedere al passaggio successivo.

Crea un host virtuale Nginx per Laravel

Successivamente, dovrai creare un file di configurazione Nginx per Laravel. Puoi crearlo usando il seguente comando:

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

Aggiungi le seguenti righe:

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

Salva e chiudi il file, quindi verifica Laravel per eventuali errori di configurazione:

nginx -t

Dovresti ottenere il seguente output:

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

Successivamente, riavvia il servizio Nginx e PHP-FPM per applicare le modifiche:

systemctl restart php-fpm
systemctl restart nginx

Puoi anche verificare lo stato di Nginx usando il seguente comando:

systemctl status nginx

Otterrai il seguente output:

? 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.

Configura firewall per Laravel

Successivamente, dovrai consentire le porte 80 e 443 attraverso il firewall firewalld. Puoi consentirli con il seguente comando:

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

Successivamente, ricarica il firewalld per applicare le modifiche:

firewall-cmd --reload

Accedi all'interfaccia utente Web di Laravel

Ora apri il tuo browser web e accedi all'interfaccia utente web di Laravel utilizzando l'URL http://laravel.exampledomain.com. Dovresti vedere la pagina predefinita di Laravel nella seguente schermata:

Abilita SSL sul sito web di Laravel

Si consiglia di abilitare SSL sul sito Web di Laravel per proteggere la connessione. Lets Encrypt fornisce un SSL gratuito per ottenere, rinnovare e gestire i certificati SSL/TLS per il tuo dominio. Innanzitutto, installa il client Certbot con il seguente comando:

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

Successivamente, esegui il seguente comando per scaricare Lets Encrypt SSL per il tuo dominio Laravel:

certbot --nginx -d laravel.exampledomain.com

Ti verrà chiesto di fornire la tua email valida e di accettare i termini di servizio come mostrato di seguito:

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): 

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

Successivamente, seleziona se reindirizzare o meno il traffico HTTP su HTTPS:

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

Digita 2 e premi invio per avviare il processo. Una volta che il certificato è stato installato, dovresti vedere il seguente output:

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.

A questo punto, il tuo sito web Laravel è protetto con Lets Encrypt SSL. Ora puoi accedervi in modo sicuro utilizzando l'URL https://laravel.exampledomain.com.

Conclusione

Congratulazioni! hai installato con successo Laravel con Nginx e Lets Encrypt SSL su Alma Linux 8. ora puoi iniziare a sviluppare applicazioni basate su PHP utilizzando il framework Laravel. Non esitate a chiedermi se avete domande.