LNMP环境是指Linux + Nginx + MySQL + PHP的组合,常用于搭建网站和应用程序。下面是在CentOS 8上手动搭建LNMP环境的步骤:
1. 更新系统
```
sudo dnf update -y
```
2. 安装Nginx
```
sudo dnf install nginx -y
```
3. 启动Nginx
```
sudo systemctl start nginx
```
4. 设置Nginx开机启动
```
sudo systemctl enable nginx
```
5. 安装MySQL
```
sudo dnf install @mysql -y
```
6. 启动MySQL
```
sudo systemctl start mysqld
```
7. 设置MySQL开机启动
```
sudo systemctl enable mysqld
```
8. 运行MySQL安全脚本以提高安全性
```
sudo mysql_secure_installation
```
9. 安装PHP及相关扩展
```
sudo dnf install php php-common php-fpm php-mysqlnd -y
```
10. 配置PHP-FPM
```
sudo vi /etc/php-fpm.d/www.conf
```
找到以下两行,并取消注释(去掉前面的分号):
```
listen.owner = nobody
listen.group = nobody
```
保存并关闭文件。
11. 启动PHP-FPM
```
sudo systemctl start php-fpm
```
12. 设置PHP-FPM开机启动
```
sudo systemctl enable php-fpm
```
13. 配置Nginx虚拟主机
```
sudo vi /etc/nginx/conf.d/default.conf
```
清空文件内容,然后添加以下内容:
```
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
将`example.com`替换为你的域名或IP地址,并设置正确的`root`路径。保存并关闭文件。
14. 重启Nginx
```
sudo systemctl restart nginx
```
至此,你已成功搭建了LNMP环境。你可以将网站文件放在`/var/www/html`目录下,并通过浏览器访问你的网站。