GNU/Linux >> LINUX-Kenntnisse >  >> Ubuntu

So installieren Sie Nginx, MySQL und PHP (LEMP) unter Ubuntu 20.04

Eine Kombination aus Linux, Nginx, MySQL und PHP ist als LEMP-Stack bekannt und die beliebte Webhosting-Umgebung für die PHP-basierte Anwendung. Hier ist Linux ein Betriebssystem, Nginx ist der beliebte Webserver, MySQL ist ein relationales Datenbankverwaltungssystem, das zum Speichern von Daten verwendet wird, und PHP ist die weit verbreitete Programmiersprache.

Dieser Artikel hilft Ihnen bei der Installation von Nginx, MySQL 8.0 und PHP 7.4 auf einem Ubuntu Linux-System. Beginnen wir mit der Installation des LEMP-Stacks auf Ihrem Ubuntu-Rechner.

Voraussetzungen

Bevor Sie mit der LEMP-Installation auf Ubuntu beginnen:

  • Ein laufendes Ubuntu 20.04-System
  • Melden Sie sich als sudo-Proviled-Konto auf Ihrem System an. Um es zu erstellen, folgen Sie dem Tutorial zur Ersteinrichtung des Servers.
  • Ein Domänen-/Subdomänenname, der auf Ihren Server verweist

Nginx-Webserver installieren

Als nächstes müssen Sie den Nginx-Webserver auf Ihrem System installieren. Die Nginx-Pakete sind unter den standardmäßigen apt-Repositories verfügbar.

Führen Sie die folgenden Befehle aus, um es zu installieren:

sudo apt update
sudo apt install nginx

Installation von PHP mit PHP-FPM

PHP 7.4-Pakete sind unter den Standard-Repositorys auf Ubuntu 20.04 LTS verfügbar. Verwenden Sie den folgenden Befehl, um den apt-Cache zu aktualisieren und PHP auf Ihrem System zu installieren.

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

Installieren Sie auch zusätzliche PHP-Module, die für Ihre Anwendung erforderlich sind.

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

Sie haben PHP 7.4 mit dem PHP FPM-Paket auf Ihrem System installiert. Lassen Sie uns den Status des PHP-FPM-Dienstes überprüfen, indem Sie den folgenden Befehl ausführen:

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.

Installieren von MySQL

Die standardmäßigen apt-Repositories von Ubuntu 20.04 enthalten MySQL Server 8.0. Installieren Sie schließlich mysql-server-Pakete für die MySQL-Datenbank. Installieren Sie außerdem das Paket php-mysql, um die MySQL-Unterstützung mit PHP zu nutzen. Verwenden Sie den folgenden Befehl, um es zu installieren.

sudo apt install mysql-server php7.4-mysql

Das Installationsprogramm fordert Sie zur Eingabe des Root-Passworts auf. Dieses Passwort funktioniert für Ihren MySQL-Root-Benutzer. Führen Sie nach der Installation von MySQL den folgenden Befehl für die Anfangseinstellungen des MySQL-Servers aus. Sie werden sehen, dass das Skript nach mehr Einstellungen fragt als frühere MySQL-Versionen, wie z. B. Richtlinien zur Kennwortvalidierung usw.

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!

Nginx mit PHP-FPM konfigurieren

Lassen Sie uns einen virtuellen Nginx-Host erstellen, der mit FPM/FastCGI ausgeführt wird. Für dieses Tutorial verwenden wir den standardmäßigen VirtualHost. Bearbeiten Sie die Konfigurationsdatei des VirtualHost-Hosts im Texteditor. Sie können gemäß Ihren Anforderungen einen neuen VirtualHost erstellen, stellen Sie also sicher, dass Sie jeden neuen VirtualHost aktivieren.

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

Verwenden Sie die unten stehende grundlegende Konfiguration des virtuellen Nginx-Hosts mit PHP-fpm-Einstellungen. Aktualisieren Sie die Konfiguration wie folgt.

Apache
123456789101112131415 server {listen 80; root /var/www/html; index index.php index.html index.htm; server_name example.com; Ort / { Versuchsdateien $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; }}

Speichern Sie Ihre Änderungen in der Konfigurationsdatei und erstellen Sie einen Link zum standortaktivierten Verzeichnis.

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

Starten Sie dann den Nginx-Dienst neu, um die Änderungen neu zu laden.

sudo systemctl restart nginx

Schritt 5 – Dienste verwalten

Wir haben die Installation des LAMP-Stacks auf dem Ubuntu 20.04 LTS-System abgeschlossen. Die folgenden Befehle helfen Ihnen beim Starten/Stoppen oder Neustarten von Nginx- und MySQL-Diensten, die mit systemd ausgeführt werden.

Um Nginx- und MySQL-Dienste neu zu starten, geben Sie Folgendes ein:

sudo systemctl restart nginx
sudo systemctl restart mysql

Geben Sie zum Starten von Nginx- und MySQL-Diensten Folgendes ein:

sudo systemctl start nginx
sudo systemctl start mysql

Um Nginx- und MySQL-Dienste zu stoppen, geben Sie Folgendes ein:

sudo systemctl stop nginx
sudo systemctl stop mysql

Schritt 6 – Firewall-Regeln anpassen

Sie können direkt einen Dienstnamen wie „http“ oder „https“ zulassen. Der Firewall verwendet die Datei /etc/services, um den entsprechenden Port des Dienstes zu ermitteln.

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

sudo firewall-cmd --reload

Schritt 7 – Überprüfen Sie die Einrichtung

Nach Abschluss aller Einstellungen. Lassen Sie uns eine info.php erstellen Datei-Website-Dokument-Root mit folgendem Inhalt.

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

Greifen Sie nun im Webbrowser auf diese Datei zu. Sie sehen den Bildschirm wie unten mit allen Details von PHP auf dem Server.

Herzlichen Glückwunsch! Sie haben den LEMP-Server erfolgreich auf Ihrem Ubuntu 20.04 LTS-System konfiguriert.


Ubuntu
  1. So installieren Sie LEMP Stack (Nginx, MySQL, PHP v7) unter CentOS 7

  2. So installieren Sie Phorum mit Nginx auf Ubuntu 18.04 LTS

  3. So installieren und konfigurieren Sie PHP und Nginx (LEMP) unter Ubuntu 20.04

  4. So installieren Sie LEMP auf Ubuntu 18.04

  5. So installieren Sie LEMP Stack Nginx, MySQL, PHP unter Ubuntu 22.04

So installieren Sie LAMP unter Ubuntu 14.04

So installieren Sie LEMP unter Ubuntu 15.10

So installieren Sie LAMP unter Ubuntu 15.10

So installieren Sie LEMP auf Ubuntu 16.04

So installieren Sie Nginx, MySQL und PHP (LEMP) auf einem Ubuntu 15.04-Server

So installieren Sie Magento 2.4.5 auf Ubuntu 22.04