在CentOS系统上搭建LNMP(Linux, Nginx, MySQL, PHP)环境是一个常见的任务,适用于部署各种Web应用。以下是详细的搭建步骤:
sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo setenforce 0
更新系统包:
sudo yum update
安装Nginx:
sudo yum install epel-release
sudo yum install nginx
启动Nginx并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
测试Nginx配置:
sudo nginx -t
重新加载Nginx配置:
sudo systemctl reload nginx
安装MySQL服务器:
sudo yum install mariadb-server mariadb
启动MySQL服务并设置开机自启动:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全脚本以设置root密码和其他安全选项:
sudo mysql_secure_installation
安装PHP及其扩展:
sudo yum install php php-fpm php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
启动PHP-FPM并设置开机自启动:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
配置Nginx支持PHP:
编辑 /etc/nginx/nginx.conf
文件,在 server
块中添加以下内容:
location ~ \.php$ {
root html;
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测试文件:
在 /usr/share/nginx/html
目录下创建 info.php
文件,内容如下:
<?php phpinfo(); ?>
访问测试文件:
在浏览器中输入服务器的IP地址,加上 /info.php
,例如 http://your_server_ip/info.php
,如果看到PHP的信息页面,说明LNMP环境搭建成功。
以上步骤涵盖了从系统配置到软件安装和配置的全过程。请根据实际网络环境和软件版本进行适当调整。