本文根据 how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04 学习而来,可以参看原文。
LEMP软件堆栈是一组软件,用于为用PHP编写的动态网页和Web应用程序提供服务。 这是一个缩写词,用于描述Linux操作系统和Nginx(发音为“ Engine-X”)Web服务器。 后端数据存储在MySQL数据库中,动态处理由PHP处理。本文介绍如何在Ubuntu 20.04服务器上安装LEMP堆栈。
前提条件:
已拥有一个非root 的sudo 权限用户。 如果没有,在root 下创建一个
adduser sammy
usermod -aG sudo sammy
这样就建立了一个sudo 权限用户
安装 Nginx
执行下面命令安装:
sudo apt update
sudo apt install nginx
查看防火墙
sudo ufw app list
可以看到
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
使能防火墙
sudo ufw allow 'Nginx HTTP'
查看状态:
sudo ufw status
可以看到:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
查看本机ip 地址
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
也可以这样:
curl -4 icanhazip.com
得到ip地址,主要是验证是否安装好了。
在浏览器里输入 ip 地址,应该可以看到:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
这样说明安装好了Nginx
安装MySQL
执行下面命令:
sudo apt install mysql-server
执行数据库安全程序
sudo mysql_secure_installation
如果回答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
其实就是限制口令要复杂一点。
测试:
sudo mysql
就进入了mysql ,因为sudo 不需要口令。exit 退出。
安装PHP
执行下面命令:
sudo apt install php-fpm php-mysql
配置Nginx 使用 PHP
在Ubuntu 20.04上,Nginx默认情况下启用了一个服务器块,并将其配置为提供/ var / www / html目录中的文档。 尽管这对于单个站点非常有效,但是如果您托管多个站点,则可能变得难以管理。 我们将在/ var / www内为your_domain网站创建一个目录结构,而不是修改/ var / www / html,如果客户请求未收到,则将/ var / www / html保留为默认目录 匹配其他任何网站。
原文中讲解了配置了多个站点,我这里简单一点,就用缺省一个站点。要看多个站点配置,可以参看原文,也可以看 nginx配置实战入门。所谓简单就是不用域名。
工作目录转到 /etc/nginx/sites-available 编辑其下的default
sudo nano default
找到下面这一段,开始的时候是全部注释了的,我这里取消了4行的注释,最后一行是: }
# pass PHP scripts to FastCGI server
#
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;
}
这里有个文件是/var/run/php/php7.4-fpm.sock ,我找到这个目录,有这个文件,以前一个教程这个文件名不一样,可能是7.2 ,与我系统不一致,结果就会报错。
保存文件,然后执行
sudo nginx -t
检查有错误否。没错误重载nginx
sudo systemctl reload nginx
配置就改好了。
测试
在/var/www/html 目录下建立一个文件index.html,
sudo nano index.html ,内容如下:
<html>
<head>
<title>your_domain website</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the landing page of <strong>your_domain</strong>.</p>
</body>
</html>
在浏览器中输入网址可以看到页面
Hello World!
This is the landing page of your_domain.
再在其下建立一个info.php
sudo nano /var/www/html/info.php
内容如下:
<?php
phpinfo();
在浏览器里输入 ip 地址/php.info
我的是http://138.197.144.170/info.php
就会显示很多php 信息的页面。
测试从php 的数据库连接(可选)
这里就不介绍了,需要可以参看原文,或者
介绍到此。