在我们之前的教程中,我们讨论了如何 在 Ubuntu 18.04 LTS 中安装 LAMP 堆栈 服务器。 在本教程中,我们将了解如何在 Ubuntu 18.04 LTS 最小服务器版中安装 Nginx、MariaDB、PHP(LEMP 堆栈)。 正如我在上一篇教程中提到的,LEMP 是 大号内克斯,Engine-x, 米咏叹调/米ySQL, 磷生命值/磷错误/磷ython。 出于本教程的目的,我将使用以下测试机器:
- 操作系统 : Ubuntu 18.04 LTS 服务器
- IP地址 : 192.168.225.22/24
让我们开始吧。
在 Ubuntu 18.04 LTS 中安装 Nginx、MariaDB、PHP(LEMP 堆栈)
1.安装Nginx
打开终端,运行以下命令安装 Nginx 网络服务器:
$ 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 Sat 2018-07-07 12:10:05 UTC; 57s ago Docs: man:nginx(8) Process: 1846 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1833 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1849 (nginx) Tasks: 2 (limit: 2322) CGroup: /system.slice/nginx.service ├─1849 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1851 nginx: worker process Jul 07 12:10:05 ubuntuserver systemd[1]: Starting A high performance web server and a reverse proxy server... Jul 07 12:10:05 ubuntuserver systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument Jul 07 12:10:05 ubuntuserver systemd[1]: Started A high performance web server and a reverse proxy server.
如果您看到类似上面的输出,则 Nginx 服务已启动。
如果它还没有启动,你可以使用命令启动它:
$ sudo systemctl start nginx
1.1 调整防火墙允许Nginx web server
默认情况下,如果您在 Ubuntu 18.04 LTS 中启用了 UFW 防火墙,则无法从远程系统访问 Nginx Web 浏览器。 您必须按照以下步骤通过 UFW 允许 http 和 https 流量。
首先,让我们使用命令查看哪些应用程序安装了配置文件:
$ sudo ufw app list Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
如您所见,Nginx 和 OpenSSH 应用程序已经安装了 UFW 配置文件。
如果您查看 “Nginx 满” 配置文件,您将看到它启用了到端口的流量 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 浏览器并导航到 https://localhost/ 或者 https://IP 地址/.
您将看到如下所示的页面。
如果你看到类似上面的输出,恭喜! Nginx 服务器正在工作!
2. 安装 MariaDB
MariaDB 是 MySQL 数据库服务器的直接替代品。
要安装它,请运行:
$ sudo apt install mariadb-server mariadb-client
Ubuntu 官方存储库中的 MariaDB 版本可能已过时。 如果要安装最新的 MariaDB,请添加 MariaDB 官方仓库 对于 Ubuntu 并安装它,如下所示。
首先,添加 MariaDB 存储库并导入密钥,如下所示。
$ sudo apt-get install software-properties-common $ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 $ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://sgp1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu bionic main'
添加存储库后,运行以下命令安装 MariaDB。
$ sudo apt update
$ sudo apt install mariadb-server
使用命令验证 MariaDB 服务是否正在运行:
$ sudo systemctl status mysql
样本输出:
● mariadb.service - MariaDB database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-07-07 12:15:13 UTC; 2min 57s ago Main PID: 3428 (mysqld) Status: "Taking your SQL requests now..." Tasks: 27 (limit: 2322) CGroup: /system.slice/mariadb.service └─3428 /usr/sbin/mysqld Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: mysql Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: performance_schema Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: Phase 6/7: Checking and upgrading tables Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: Processing databases Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: information_schema Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: performance_schema Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: Phase 7/7: Running 'FLUSH PRIVILEGES' Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3465]: OK Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3534]: Checking for insecure root accounts. Jul 07 12:15:14 ubuntuserver /etc/mysql/debian-start[3538]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables
mysql正在运行!
2.1 设置数据库管理用户(root)密码
在 MariaDB 安装期间,它将为管理用户帐户 (root) 设置密码。
如果您尝试使用命令手动设置密码:
$ mysql_secure_installation
不能登录设置密码。 您将看到如下所示的错误。
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): ERROR 1698 (28000): Access denied for user 'root'@'localhost' Enter current password for root (enter for none):
要解决此问题,请使用以下命令以 MySQL 数据库管理员身份登录:
$ sudo mysql -u root
登录到 MySQL 提示符后,一一运行以下命令。
use mysql;
update user set plugin='' where User="root";
flush privileges;
q
现在,您可以使用命令设置数据库管理密码:
$ mysql_secure_installation
Enter 密码密码,然后按 ENTER 键接受默认值。
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): # Press ENTER OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] # Press ENTER New password: # Enter password Re-enter new password: # Re-enter password Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n] # Press ENTER ... 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? [Y/n] # Press ENTER ... Success! By default, MariaDB 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? [Y/n] # Press ENTER - Dropping test database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - 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? [Y/n] # Press ENTER ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
就是这样。 已设置数据库管理用户帐户的密码。
3.安装PHP
要安装 PHP,请运行:
$ sudo apt-get install php-fpm php-mysql
安装 PHP 后,我们需要通过简单的更改来保护它。
为此,请编辑 php.ini 文件:
$ sudo vi /etc/php/7.2/fpm/php.ini
找到以下行:
;cgi.fix_pathinfo=1
取消注释并更改其值 1 到 0(零).
cgi.fix_pathinfo=0
Save 和 close 文件。 然后,重新启动 PHP-FPM 服务以使更改生效。
$ sudo systemctl restart php7.2-fpm
使用命令检查 PHP-FPM 服务是否正在运行:
$ sudo systemctl status php7.2-fpm
样本输出:
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-07-07 12:23:46 UTC; 7s ago Docs: man:php-fpm7.2(8) Main PID: 11005 (php-fpm7.2) Status: "Ready to handle connections" Tasks: 3 (limit: 2322) CGroup: /system.slice/php7.2-fpm.service ├─11005 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf) ├─11018 php-fpm: pool www └─11019 php-fpm: pool www Jul 07 12:23:46 ubuntuserver systemd[1]: Starting The PHP 7.2 FastCGI Process Manager... Jul 07 12:23:46 ubuntuserver systemd[1]: Started The PHP 7.2 FastCGI Process Manager.
3.1 配置 Nginx 使用 PHP-FPM
我们需要配置 Nginx 以使用 PHP-FPM。
为此,请编辑 Nginx 的默认虚拟主机(服务器块) /etc/nginx/sites-available/default 文件:
$ sudo vi /etc/nginx/sites-available/default
找到服务器部分,并设置您的 Ubuntu 服务器的 FQDN 或 IP 地址,如下所示。 此外,请仔细检查您是否添加了 索引.php 线。 所有更改均以粗体字显示。
[...] 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.22; [...]
让我们分解以上几行,看看每一行的含义。
- 听 80; –> 监听 ipv4。
- 听 [::]:80 默认服务器; –> 监听 ipv6。
- 根 /var/www/html; –> Nginx 文档根目录。
- server_name 192.168.225.22; –> 我们的 Ubuntu 服务器的 IP 地址。
然后,向下滚动一点并找到该部分 #location ~ .php$.
取消注释并修改以下行,如下所示。
location ~ .php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php7.2-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; } }
Save 并退出文件。
支付 close 修改时注意 fastcgi_pass 指令内 位置 ~ .php$ 堵塞。 您必须提及正确的名称文件以及实际存储在 /运行/php 服务器上的目录。 要验证它,请运行:
$ ls /run/php/ php7.2-fpm.pid php7.2-fpm.sock
如您所见,文件的名称是 php7.2-fpm.sock, 不是 php7.0-fpm.sock,这是在 位置 ~ .php$ 指示。 确保您在此指令中提到了正确的名称。
使用以下命令检查 Nginx 配置文件是否有任何语法错误:
$ sudo nginx -t
样本输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
如果您没有看到任何错误,请重新启动 nginx 服务以使更改生效。
为此,请运行:
$ sudo systemctl restart nginx
现在,让我们创建一个示例 PHP 文件来通过浏览器测试 PHP 配置。
为此,请在 Nginx 文档根文件夹下创建一个名为 info.php 的文件。
$ sudo vi /var/www/html/info.php
添加以下行:
<?php phpinfo(); ?>
Save 并退出文件。 重新启动 nginx 服务以使更改生效。
然后,打开您的网络浏览器并导航到 https://IP地址/info.php.
您将看到 PHP 详细信息。
恭喜! PHP 正在运行!!
3.2 安装 PHP 模块
为了改进 PHP 的功能,您可以安装一些额外的 PHP 模块。
要列出可用的 PHP 模块,请运行:
$ sudo apt-cache search php- | less
样本输出:
搜索 PHP 模块
使用 和 箭头在结果之间上下移动。 要退出结果,请键入 q.
要查找任何特定 php 模块的详细信息,对于 example php-gd, 跑:
$ sudo apt-cache show php-gd
要安装 php 模块,请运行:
$ sudo apt-get install php-gd
要安装所有模块(虽然不是必需的),请运行:
$ sudo apt-get install php*
安装任何 php 模块后不要忘记重新启动 Nginx 服务。
恭喜! 我们已经在 Ubuntu 18.04 LTS 服务器中成功设置了 LEMP 堆栈。 开始在您的新 LEMP Web 堆栈中部署网站和 Web 应用程序。
干杯!
感谢您的光临!
帮助我们帮助您:
- 订阅我们的电子邮件通讯: 立即注册
- 支持 OSTechNix: 通过贝宝捐款
- 下载免费的电子书和视频: TradePub 上的 OSTechNix
- 联系我们: Facebook | Twitter | 谷歌加 | 领英 | RSS订阅
祝你有美好的一天!!