如何在Ubuntu 18.04上安装LEMP

时间:2020-03-05 15:27:59  来源:igfitidea点击:

LEMP与经典LAMP的组件堆栈非常相似,只是Web服务器Apache被Nginx替换为发音为Engine-x,解释了LEMP上的'E'。
Nginx是用于HTTP,HTTPS和某些其他协议的开源反向代理服务器,它还可以用作负载平衡器,HTTP缓存和Web服务器(原始服务器)。
我们可以链接MySql或者MariaDB数据库,但在本教程中我们将对数据库使用MySql。

本教程将向我们展示在Ubuntu 18.04服务器上安装LEMP的步骤。

第1步-安装和配置Web服务器Nginx

如果我们是第一次启动Ubuntu 18.04服务器,请确保更新软件包的缓存。

# apt update

现在我们可以安装Nginx

# apt install nginx
Reading state information... Done
The following additional packages will be installed:
 fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip libnginx-mod-http-image-filter
 libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core

我们可以检查Web服务器的状态

# 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 Sun 2016-06-10 04:51:51 UTC; 2min 0s ago
     Docs: man:nginx(8)
...
...

现在确保如果重启,nginx将与系统同时启动

# systemctl enable nginx.service
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

现在,如果我们正在运行UFW防火墙,则需要允许连接到Nginx。
不同的应用程序可以在安装时(例如Nginx)向UFW注册其配置文件,因此我们可以按以下方式进行检查

# ufw app list
Available applications:
 Nginx Full
 Nginx HTTP
 Nginx HTTPS
 OpenSSH

我们可以使用名称和要使用的协议(http或者https)来启用它

# ufw allow 'Nginx HTTP'
Rule added
Rule added (v6)

现在,我们可以如下检查防火墙上允许的服务

# ufw status
Status: active
To                         Action      From
--                         ------      ---
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
Postfix                    ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)             
Postfix (v6)               ALLOW       Anywhere (v6)

现在,我们可以在浏览器中检查Web服务器

步骤2-安装和配置MySql数据库

现在,我们可以在Ubuntu 18.04上安装数据库管理MySql。

# apt install mysql-server
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-6 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
.....
.....

然后运行以下命令以保护MySql服务器。
它将询问我们是否要配置VALIDATE PASSWORD PLUGIN以设置安全的mysql根密码

# 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

它将显示三个级别的密码强度。
选择最适合一种,但请务必牢记这一点,以免出现某些访问错误。

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

现在我们可以输入数据库根密码

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

我们将被问到其他一些安全性问题:

  • 删除匿名用户? (按y | Y表示是,按其他任何键表示否):Y
  • 禁止远程root登录? (按y | Y表示是,按其他任何键表示否):Y
  • 删除测试数据库并访问它? (按y | Y表示是,按其他任何键表示否):Y
  • 现在重新加载特权表? (按y | Y表示是,按其他任何键表示否):Y

现在,我们可以使用以下命令连接到数据库

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)

但是我们应该注意,我们的数据库中仍然存在一些安全漏洞,因为在运行MySQL 5.7(及更高版本)的Ubuntu系统中,默认情况下,MySQL根用户设置为使用auth_socket插件而不是使用密码进行身份验证。
它对通过Unix套接字文件从本地主机连接的客户端进行身份验证。
我们可以检查MySql版本

# mysql --version
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper

现在,我们可以检查每个MySQL用户帐户的身份验证方法。
首先,连接数据库

# mysql

请注意,它并没有像我们上面配置的那样要求我们提供数据库根密码。
这意味着访问服务器的每个人都可以编辑数据库。
现在检查身份验证方法

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *4A9F410D01A83957975233C9B87881861B0CDF9C | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

我们可以看到根数据库用户的身份验证方法不是密码。
我们可以如下更改

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-db-root-password';
Query OK, 0 rows affected (0.00 sec)

现在刷新特权

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

现在,我们可以检查身份验证方法是否已更改

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *BCF4F28E525ED7EE4664FFFF4DAE13EC14A6ABE1 | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *4A9F410D01A83957975233C9B87881861B0CDF9C | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

现在我们可以退出

mysql> exit
Bye

再次检查以仅使用mysql命令登录到mysql数据库

# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

此消息告诉我们出了什么问题。
这意味着我们可能已经为数据库中的用户root设置了密码,并且需要使用以下命令输入该密码

# mysql --password
Enter password:

或者

# mysql -u root -p
Enter password:

在第二个命令中,请注意,“-u root”指示用于连接数据库的数据库用户(root)。
这意味着如果我们有paul或者john用户,则可以使用它代替root。

现在,数据库是最安全的。

第3步-安装PHP并配置PHP-FPM设置

由于Nginx不包含本地PHP处理,因此我们必须安装php-fpm(FastCGI进程管理器)和php-mysql,以允许PHP与MySQL数据库进行通信。

apt install php-fpm php-mysql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libsodium23 php-common php7.2-cli php7.2-common php7.2-fpm php7.2-json php7.2-mysql php7.2-opcache php7.2-readline
Suggested packages:
  php-pear

我们可以检查php版本

# php --version
PHP 7.2.5-0ubuntu0.18.04.1 (cli) (built: Jan 9 2016 17:21:02) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2016 Zend Technologies
 with Zend OPcache v7.2.5-0ubuntu0.18.04.1, Copyright (c) 1999-2016, by Zend Technologies

现在打开PHP-FPM默认文件以编辑以下内容

# vim /etc/php/7.2/fpm/php.ini
cgi.fix_pathinfo=0
date.timezone = Africa/Douala

保存文件并重新启动php-fpm

# systemctl restart php7.2-fpm

根据我们安装的Nginx和PHP版本,我们可能需要手动配置Nginx将连接到的PHP套接字的位置。
因此,列出目录“/var/run/php /”的内容

# ls /var/run/php/
php7.2-fpm.pid php7.2-fpm.sock

我们可以看到套接字是php7.2-fpm.sock。
现在,我们可以打开Nginx站点配置文件以将PHP用于动态内容,并且应该指出套接字文件。

# https://wiki.debian.org/Nginx/DirectoryStructure
## In most cases, administrators will remove this file from sites-enabled/and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
## This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
## Please see /usr/share/doc/nginx-doc/examples/for more detailed examples.# Default server configuration
#server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name www.linoserver.com;
        location/{
                try_files $uri $uri/=404;
        }    
        location ~ \.php${
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www/html;
        }
        location ~ /\.ht {
                deny all;
        }    
}

现在,我们可以检查Nginx配置文件的语法,并在文件中指示具有良好的PHP套接字。

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

现在我们可以创建一个php文件来检查配置

# vim /var/www/html/info.php
<?php
phpinfo();

现在我们可以检查配置

现在,我们可以删除php测试文件。