如何在Ubuntu 20.04中安装Linux,Nginx,MariaDB,PHP(LEMP Stack)
LEMP堆栈是一种技术堆栈,代表Linux,Nginx,MariaDB和PHP。 它广泛用于托管规模较小的网站/博客。
在这里,我们将看到如何在Ubuntu 20.04上安装LEMP Stack。
安装LEMP堆栈
安装Linux
这是有关逐步安装Ubuntu 20.04以及将Ubuntu 18.04或Ubuntu 19.10升级到Ubuntu 20.04的教程。
继续在Ubuntu 20.04上安装EMP(Nginx v1.17.10,PHP v7.4,MariaDB v10.3)软件包。
安装Nginx
Nginx是一个免费的高性能Web服务器,用于提供静态Web内容。 它以其稳定性,简单的配置和低资源消耗而著称。
从Ubuntu存储库安装Nginx
使用以下命令安装Nginx软件包。 默认情况下,Ubuntu 20.04随附Nginx v1.17.10。
sudo apt update
sudo apt install -y nginx
打开Web浏览器并访问您的服务器IP地址。
http:// your-ip-add-ress
您应该看到Nginx的默认页面,这确认Nginx Web服务器已成功安装并正常工作。
Nginx的默认页面
Nginx在Ubuntu 20.04上的默认文档根目录是/ var / www / html,其配置文件位于/ etc / nginx /目录下。
从Nginx存储库安装Nginx
Nginx为Ubuntu操作系统提供了存储库。 Nginx的官方存储库发布了v1.17.11。
使用以下命令从Nginx的存储库安装Nginx。
sudo apt update
sudo apt install -y curl gnupg2 ca-certificates lsb-release
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
echo "deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu focal nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
使用以下命令安装Nginx软件包。
sudo apt update
sudo apt install -y nginx
安装后启动Nginx服务。
sudo systemctl start nginx
打开Web浏览器并访问您的服务器IP地址。
http:// your-ip-add-ress
您应该看到Nginx的默认页面,这确认Nginx Web服务器已成功安装并正常工作。

Nginx的默认页面
Nginx在Ubuntu 20.04上的默认文档根目录是/ usr / share / nginx / html,其配置文件位于/ etc / nginx /目录下。
安装MariaDB服务器
使用以下命令安装MariaDB服务器。 默认情况下,Ubuntu 20.04附带了MariaDB v10.3。
读: 如何在Ubuntu 20.04上安装MariaDB-官方存储库
sudo apt install -y mariadb-server mariadb-client
您还可以为LEMP堆栈安装MySQL 8.0。
接下来,设置MariaDB根密码,并使用mysql_secure_installation命令保护MariaDB实例的安全。
sudo mysql_secure_installation
Output: 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): 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] Y New password: xxx Re-enter new password: xxx 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] 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? [Y/n] Y ... 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] 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? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
安装PHP-FPM
现在,安装PHP-FPM(PHP-FastCGI进程管理器)以显示用PHP编写的动态内容。
使用以下命令安装PHP-FPM。 默认情况下,Ubuntu 20.04附带PHP-FPM v7.4。
sudo apt install -y php-fpm php-mysql php-cli
默认情况下,PHP-FPM侦听套接字/run/php/php7.4-fpm.sock。
要使PHP-FPM使用TCP连接,请编辑以下文件。
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
然后,更改listen参数。
从:
listen = /run/php/php7.4-fpm.sock
至:
listen = 127.0.0.1:9000
重新启动PHP-FPM进程。
sudo systemctl restart php7.4-fpm
测试LEMP堆栈
让我们在Nginx服务器上创建一个基于名称的虚拟主机,以测试我们的LEMP堆栈安装。
域名: site.itzgeek.local
文件根: /www/site.itzgeek.local
在/etc/nginx/conf.d/目录中为我们的域创建一个虚拟主机配置文件。
sudo nano /etc/nginx/conf.d/site.itzgeek.local.conf
添加以下内容。
server { server_name site.itzgeek.local; root /www/site.itzgeek.local; location / { index index.html index.htm index.php; } location ~ .php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
创建用于放置PHP文件的根目录。
sudo mkdir -p /www/site.itzgeek.local
为了测试PHP-FPM支持,请将.php文件放置在创建的虚拟主机的文档根目录上。
echo "<?php phpinfo(); ?>" | sudo tee /www/site.itzgeek.local/index.php
更改根目录的所有权。
sudo chown -R www-data:www-data /www/site.itzgeek.local/
重新启动Nginx服务。
sudo systemctl restart nginx
在您的服务器或台式机上,如果您的环境中没有用于名称解析的DNS服务器,请在/ etc / hosts文件中为您的域(site.itzgeek.local)创建一个主机条目。
sudo nano /etc/hosts
添加主机条目如下所示。
192.168.0.10 site.itzgeek.local site
打开您的网络浏览器,然后在网址中键入您的域名。
http://site.itzgeek.local
该页面如下所示:

PHP信息
从上面的屏幕截图中,PHP正在通过 FPM /快速CGI,如 服务器API 线。
如果向下滚动页面,则可以查看所有受支持的PHP扩展详细信息。

MySQL扩展
结论
就这样。 我希望您了解了如何在Ubuntu 20.04上安装LEMP堆栈。 请在评论部分分享您的反馈。