在Ubuntu 20.04 LTS上安装Nginx,MySQL,PHP(LEMP堆栈)
在我们以前的教程中,我们讨论了如何在Ubuntu 20.04 LTS服务器中安装 LAMP 堆栈。
在本教程中,我们将看到如何在Ubuntu 20.04 LTS服务器版本上安装Nginx,MySQL,PHP(LEMP堆栈)。
LEMP是Linux,Engine-X,MariaDB/MySQL,PHP/Perl/Python的首字母缩略词。
在Ubuntu 20.04 LTS上安装Nginx,MySQL,PHP(LEMP堆栈)
出于本教程的目的,我将使用以下测试机器:
- 操作系统:Ubuntu 20.04 LTS服务器
- IP地址:192.168.225.52/24
让我们开始。
1.在Ubuntu上安装nginx
从终端运行以下命令以安装nginx webserver:
$sudo apt install nginx
安装nginx后,检查nginx服务是否正在运行或者不使用命令:
$sudo systemctl status nginx
示例输出:
● 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 Fri 2017-05-29 10:13:47 UTC; 56s ago Docs: man:nginx(8) Main PID: 1349 (nginx) Tasks: 2 (limit: 2283) Memory: 4.4M CGroup: /system.slice/nginx.service ├─1349 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1350 nginx: worker process Jan 29 10:13:46 ubuntuserver systemd[1]: Starting A high performance web server and a reverse proxy server... Jan 29 10:13:47 ubuntuserver systemd[1]: Started A high performance web server and a reverse proxy server.
如果我们看到如上所述的输出,则已启动NGINX服务。
如果尚未启动nginx服务,则可以使用命令启动它:
$sudo systemctl enable nginx
$sudo systemctl start nginx
1.1通过UFW防火墙允许Nginx Web服务器
UFW,代表简单的防火墙,是一个管理NetFilter防火墙的程序,旨在易于使用。
默认情况下,UFW可用于所有Ubuntu版本。
默认情况下,如果我们在Ubuntu 20.04 LTS中启用了UFW防火墙,则无法从远程系统访问NginX Web浏览器。
我们必须按照下面给出的步骤允许HTTP和HTTPS端口。
首先,让我们查看使用命令安装了配置文件的应用程序:
$sudo ufw app list
示例输出:
Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
正如我们所看到的,nginx和openssh应用程序已安装UFW配置文件。
如果我们查看"nginx full"配置文件,我们将看到它使流量能够到端口80和443:
$sudo ufw app info "Nginx Full"
示例输出:
Profile: Nginx Full Title: Web Server (Nginx, HTTP + HTTPS) Description: Small, but very powerful and efficient web server Ports: 80,443/tcp
现在,运行以下命令以允许此配置文件传入HTTP和HTTPS流量:
$sudo ufw allow in "Nginx Full"
我们将看到下面的输出:
Rules updated Rules updated (v6)
如果要允许HTTPS流量,但只有HTTP(80)流量,运行:
$sudo ufw app info "Nginx HTTP"
让我们继续运行nginx测试页。
为此,请打开Web浏览器并导航到http://localhost /或者http://ip-address /。
我们将看到Nginx欢迎页面,如下所示。
恭喜! nginx服务器正在工作!
2.在Ubuntu上安装mysql
要在Ubuntu上安装mysql,请运行:
$sudo apt install mysql-server
验证MySQL服务是否正在运行或者不使用命令:
$sudo systemctl status mysql
示例输出:
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2017-05-29 10:31:56 UTC; 51s ago Main PID: 2288 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 2283) Memory: 325.6M CGroup: /system.slice/mysql.service └─2288 /usr/sbin/mysqld Jan 29 10:31:49 ubuntuserver systemd[1]: Starting MySQL Community Server... Jan 29 10:31:56 ubuntuserver systemd[1]: Started MySQL Community Server.
当我们在上面的输出中看到,MySQL正在运行!
2.1 MySQL的设置数据库管理用户(root)密码
默认情况下,MySQL root用户密码为空。
建议不要为数据库管理帐户使用空密码。
我们需要通过运行以下脚本来保护MySQL服务器:
$sudo mysql_secure_installation
将询问我们是否要设置"验证密码"组件。
此组件允许用户为数据库凭据配置强密码。
如果已启用,它将自动检查密码的强度,并强制执行用户只设置足够安全的密码。
将其禁用是安全的。
但是,我们必须使用强大而独特的数据库凭据密码。
如果我们不想启用此组件,只需按任意键即可跳过密码验证部分并继续其余步骤。
如果答案是肯定的,我们将被要求选择密码验证级别。
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN 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 plugin? Press y|Y for Yes, any other key for No y
可用的密码验证低,中等和强。
只需输入适当的数字(0为低,为中频为1,为强密码),然后点击Enter键。
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: 0
现在,输入MySQL root用户的密码。
请注意,我们必须根据我们在上一步中选择的密码策略使用MySQL root用户的密码。
如果我们没有启用插件,只需使用我们选择的任何强大和唯一密码。
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
一旦输入密码两次,我们将看到密码强度(在我们的情况下是100)。
如果它对我们可以,请按Y继续提供的密码。
如果不满意密码长度,请按任何其他键并设置强密码。
我对当前的密码好,所以我选择了y。
对于剩下的问题,只需键入y并按Enter键。
这将删除匿名用户,禁止远程删除root用户登录并删除测试数据库。
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!
完毕!我们已经设置了MySQL root用户的密码。
2.2更改MySQL root用户的身份验证方法
默认情况下,MySQL root用户将使用从MySQL 5.7和Ubuntu上的较新版本开始使用"auth_socket"插件进行身份验证。
即使它增强了安全性,它也将使用任何外部程序访问数据库服务器时,它也将复杂化,例如PHPMyAdmin。
要解决此问题,我们需要将Auth_socket的身份验证方法更改为"caching_sha2_password"或者"mysql_native_password"。
正如MySQL 8.0版本,首选和默认身份验证插件是CACHING_SHA2_PASSWORD。
CACHING_SHA2_PASSWORD认证插件提供比MYSQL_NITIVE_PASSWORD插件更安全的密码加密。
要使用命令更改身份验证插件,请使用命令登录MySQL提示符:
$sudo mysql
在MySQL提示符下运行以下命令以查找所有MySQL用户帐户的当前身份验证方法:
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
示例输出:
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $Amysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Password123#@!';5$=s%UO"�Hyman@theitroad>Hyman@theitroad | caching_sha2_password | localhost | | mysql.infoschema | $Amysql> FLUSH PRIVILEGES;5$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $Amysql> SELECT user,authentication_string,plugin,host FROM mysql.user;5$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $Amysql> exit5$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | | auth_socket | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)
如我们所见,MySQL root用户使用auth_socket插件进行身份验证。
要将auth_socket插件更改为caching_sha2_password,请在mysql提示符下运行以下命令。
如果已启用验证密码插件,请确保根据当前的策略要求使用强密码。
强势应该由至少8个字符组成,包括大写字母,小写字母,数字和特殊字符。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
替换"password123#@!"在上面的命令中拥有强大而独特的密码。
使用命令更新更改:
mysql> FLUSH PRIVILEGES;
现在检查当前的身份验证插件是否已更改或者不使用命令:
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
示例输出:
好的!现在MyQL root用户可以使用密码进行身份验证以访问MySQL shell。
退出MySQL提示符:
exit
说明:
尽管CACHING_SHA2_PASSWORD插件提供增强的安全加密,但它具有带有现有MYSQL安装的兼容性问题。
有关详细信息,请参阅此链接。
如果我们遇到任何兼容性问题,则需要将"mysql_native_password"插件设置为默认身份验证插件。
要更改为mysql_native_password插件,请在mysql提示符下运行以下命令。
$sudo apt-get install php-fpm php-mysql
使用命令更新更改:
$sudo vi /etc/php/7.4/fpm/php.ini
现在,如果已更改身份验证方法或者不使用命令,请再次检查:
;cgi.fix_pathinfo=1
示例输出:
通过输入以下命令退出MySQL提示符:
cgi.fix_pathinfo=0
3.在Ubuntu上安装PHP
要安装PHP,请运行:
$sudo systemctl restart php7.4-fpm
安装PHP后,我们需要通过在PHP.ini文件中进行简单的更改来保护它。
为此,请使用我们喜欢的编辑器编辑PHP.ini文件:
$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: en> Active: active (running) since Fri 2017-05-29 10:40:45 UTC; 11s ago Docs: man:php-fpm7.4(8) Process: 10711 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/ph> Main PID: 10693 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 2283) Memory: 7.0M CGroup: /system.slice/php7.4-fpm.service ├─10693 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─10709 php-fpm: pool www └─10710 php-fpm: pool www Jan 29 10:40:44 ubuntuserver systemd[1]: Starting The PHP 7.4 FastCGI Process Manager... Jan 29 10:40:45 ubuntuserver systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
取消注释它并将其值从1到0(零)更改。
$sudo vi /etc/nginx/sites-available/default
保存并关闭文件。
然后,重新启动PHP-FPM服务以生效更改。
[...] server { listen 80 default_server; listen [::]:80 default_server; [...] root /var/www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name 192.168.225.52; [...]
检查PHP-FPM服务是否正在运行或者不使用命令:
location ~ \.php${ include snippets/fastcgi-php.conf; ## # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }
示例输出:
$ls /var/run/php/ php-fpm.sock php7.4-fpm.pid php7.4-fpm.sock
3.1配置nginx使用php-fpm
我们需要配置nginx来使用php-fpm。
为此,请编辑nginx的默认vhost(服务器块)/etc/nginx/sites可用/默认文件:
$sudo nginx -t
找到服务器部分,并设置Ubuntu Server的FQDN或者IP地址,如下所示。
此外,请仔细检查是否添加了index.php行。
所有更改都以粗体字母显示。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
其中
- 听80; - >倾听IPv4.
- 听[::]:80 default_server; - >听取IPv6.
- index.php - >如果使用PHP,请添加此。
- root/var/www/html; - > nginx文档根目录。
- server_name 192.168.225.52; - >我们的Ubuntu Server的IP地址。
然后,向下滚动一点点并找到"#location~\ .php $"部分。
取消注释并修改以下行,如下所示。
$sudo systemctl restart nginx
请参阅以下屏幕截图。
更改标记为/带有红色。
保存并退出文件。
在修改FastCGI_pass指令时要密切关注~\ .php $块。
我们必须使用实际存储在服务器上的/var /运行/php目录中的正确名称文件。
要验证它,请运行:
$sudo vi /var/www/html/info.php
如我们所见,文件的名称是PHP7.4-FPM.SOCK。
确保在此指令中提到了正确的名称。
检查nginx配置文件是否有使用命令的任何语法错误:
<?php phpinfo(); ?>
示例输出:
$sudo systemctl restart nginx
如果我们没有看到任何错误,请重新启动nginx服务以生效更改。
为此,运行:
$sudo apt-cache search php- | less
现在,让我们创建一个示例PHP文件以通过浏览器测试PHP配置。
为此,请在nginx文档根文件夹下创建一个名为"info.php"的文件。
$sudo apt-cache show php-gd
添加以下行:
$sudo apt-get install php-gd
保存并退出文件。
重新启动nginx服务以生效更改。
$sudo apt-get install php*
然后,打开Web浏览器并导航到http://ip-address/info.php。
我们将看到PHP详细信息。
恭喜! PHP正在工作!
3.2安装PHP模块
为了提高PHP的功能,我们可以安装一些另外的PHP模块。
要列出可用的PHP模块,请运行:
##代码##示例输出:
使用和箭头在结果之间上下移动。
退出,键入q。
要查找任何特定PHP模块的详细信息,例如PHP-GD,运行:
##代码##要安装PHP模块运行:
##代码##要安装所有模块(但不必要),运行:
##代码##安装任何PHP模块后,请勿忘记重新启动nginx服务。