GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Come installare Nginx, MySQL e PHP (LEMP) su Ubuntu 20.04

Una combinazione di Linux, Nginx, MySQL e PHP è nota come LEMP stack è il popolare ambiente di web hosting per l'applicazione basata su PHP. Qui Linux è un sistema operativo, Nginx è il popolare server web, MySQL è un sistema di gestione di database relazionali utilizzato per l'archiviazione dei dati e PHP è il linguaggio di programmazione ampiamente utilizzato.

Questo articolo ti aiuterà a installare Nginx, MySQL 8.0 e PHP 7.4 sul sistema Ubuntu Linux. Iniziamo con l'installazione di LEMP impila la tua macchina Ubuntu.

Prerequisiti

Prima di iniziare l'installazione di LEMP su Ubuntu:

  • Un sistema Ubuntu 20.04 in esecuzione
  • Accedi come account fornito da sudo sul tuo sistema. Per crearlo, segui il tutorial di configurazione iniziale del server.
  • Un nome di dominio/sottodominio puntato al tuo server

Installazione del server Web Nginx

Successivamente, devi installare il server web Nginx sul tuo sistema. I pacchetti Nginx sono disponibili nei repository apt predefiniti.

Esegui i seguenti comandi per installarlo:

sudo apt update
sudo apt install nginx

Installazione di PHP con PHP-FPM

I pacchetti PHP 7.4 sono disponibili nei repository predefiniti su Ubuntu 20.04 LTS. Usa il comando seguente per aggiornare apt cache e installare PHP sul tuo sistema.

sudo apt update
sudo apt install -y php7.4 php7.4-fpm

Installa anche moduli PHP aggiuntivi richiesti per la tua applicazione.

sudo apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml

Hai installato PHP 7.4 con il pacchetto PHP FPM sul tuo sistema. Controlliamo lo stato del servizio PHP FPM eseguendo il comando seguente:

sudo systemctl status php7.4-fpm

● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-16 05:15:57 UTC; 34s ago
       Docs: man:php-fpm7.4(8)
    Process: 882716 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74>
   Main PID: 882699 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2283)
     Memory: 10.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─882699 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─882714 php-fpm: pool www
             └─882715 php-fpm: pool www

Jun 16 05:15:57 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Jun 16 05:15:57 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Installazione di MySQL

I repository apt di Ubuntu 20.04 predefiniti contengono il server MySQL 8.0. Infine, installa i pacchetti mysql-server per il database MySQL. Inoltre, installa il pacchetto php-mysql per usare il supporto MySQL usando PHP. Usa il comando seguente per installarlo.

sudo apt install mysql-server php7.4-mysql

Il programma di installazione richiederà la password di root, questa password funzionerà per il tuo utente root MySQL. Dopo aver installato MySQL, eseguire il comando seguente per le impostazioni iniziali del server MySQL. Vedrai che lo script richiederà più impostazioni rispetto alle versioni precedenti di MySQL come la politica di convalida della password ecc.

sudo mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component? 

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Configura Nginx con PHP-FPM

Creiamo un host virtuale Nginx da eseguire con FPM/FastCGI. Per questo tutorial, utilizziamo VirtualHost predefinito. Modifica il file di configurazione dell'host VirtualHost nell'editor di testo. Puoi creare un nuovo VirtualHost secondo le tue esigenze, quindi assicurati di abilitare qualsiasi nuovo VirtualHost.

sudo vim /etc/nginx/sites-available/example.com

Usa la configurazione di base dell'host virtuale Nginx di seguito con le impostazioni di php fpm. Aggiorna la configurazione come segue.

Apache
123456789101112131415 server { ascolta 80; radice /var/www/html; indice index.php index.html index.htm; nome_server esempio.com; posizione / { file_prova $uri $uri/ =404; } posizione ~ \.php$ { include frammenti/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; }}

Salva le modifiche nel file di configurazione e crea un collegamento alla directory abilitata per il sito.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com 

Quindi riavvia il servizio Nginx per ricaricare le modifiche.

sudo systemctl restart nginx

Passaggio 5:gestione dei servizi

Abbiamo finito con l'installazione dello stack LAMP sul sistema Ubuntu 20.04 LTS. I comandi seguenti ti aiuteranno ad avviare/arrestare o riavviare i servizi Nginx e MySQL in esecuzione con systemd.

Per riavviare i servizi Nginx e MySQL, digita:

sudo systemctl restart nginx
sudo systemctl restart mysql

Per avviare i servizi Nginx e MySQL, digita:

sudo systemctl start nginx
sudo systemctl start mysql

Per interrompere i servizi Nginx e MySQL, digita:

sudo systemctl stop nginx
sudo systemctl stop mysql

Passaggio 6:modifica le regole del firewall

Puoi fornire direttamente un nome di servizio come "http" o "https" per consentire. Il firewalld usa il file /etc/services per determinare la porta corrispondente del servizio.

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

Fase 7:verifica della configurazione

Dopo aver completato tutta la configurazione. Creiamo un info.php file radice del documento del sito Web con il seguente contenuto.

sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Ora accedi a questo file nel browser web. Vedrai la schermata come sotto con tutti i dettagli di PHP sul server.

Congratulazioni! Hai configurato correttamente il server LEMP sul tuo sistema Ubuntu 20.04 LTS.


Ubuntu
  1. Come installare lo stack LEMP (Nginx, MySQL, PHP v7) su CentOS 7

  2. Come installare Phorum con Nginx su Ubuntu 18.04 LTS

  3. Come installare e configurare PHP e Nginx (LEMP) su Ubuntu 20.04

  4. Come installare LEMP su Ubuntu 18.04

  5. Come installare LEMP Stack Nginx, MySQL, PHP su Ubuntu 22.04

Come installare LAMP su Ubuntu 14.04

Come installare LEMP su Ubuntu 15.10

Come installare LAMP su Ubuntu 15.10

Come installare LEMP su Ubuntu 16.04

Come installare Nginx, MySQL e PHP (LEMP) su un server Ubuntu 15.04

Come installare Magento 2.4.5 su Ubuntu 22.04