“LNMP环境搭建实战指南:从零开始配置CentOS 7下的Nginx、MySQL与PHP“
以下是一个简化的LNMP环境搭建指南,包括安装Nginx、MySQL和PHP,并配置PHP与MySQL的通信。
# 安装Nginx
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
# 安装MySQL
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
# 安装PHP
sudo yum install php php-fpm -y
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
# 配置Nginx与PHP处理PHP文件
sudo vi /etc/nginx/nginx.conf
# 在http块中添加以下内容
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 重启Nginx
sudo systemctl restart nginx
# 测试PHP安装
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/index.php
# 打开浏览器访问服务器IP,应该可以看到PHP信息页面
以上代码提供了一个基本的LNMP环境搭建指南,包括安装Nginx、MySQL和PHP,并配置Nginx以处理PHP请求。在配置Nginx时,确保修改了正确的fastcgi\_pass参数以连接到PHP-FPM的正确端口。
评论已关闭