CentOS7.2 搭建LNMP环境完整指南

CentOS7.2系统LNMP环境搭建全流程

环境准备与系统更新

执行系统更新确保组件兼容性:

yum update -y && reboot

Nginx服务部署

安装EPEL扩展源

yum install epel-release -y

安装与启动Nginx

yum install nginx -y
systemctl start nginx
systemctl enable nginx

防火墙配置

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

MySQL数据库安装

添加MariaDB官方源

cat <<EOF > /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

安装与安全配置

yum install MariaDB-server MariaDB-client -y
systemctl start mariadb
mysql_secure_installation

PHP环境配置

添加Remi软件源

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
yum-config-manager --enable remi-php74

安装核心组件

yum install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring -y

配置PHP-FPM

sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini
systemctl start php-fpm
systemctl enable php-fpm

Nginx与PHP集成

创建测试站点配置

cat <<EOF > /etc/nginx/conf.d/test.conf
server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
    
    location / {
        index index.php index.html;
    }

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
EOF

验证配置并重启服务

nginx -t
systemctl restart nginx

环境验证测试

创建PHP探针文件

echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php

访问性测试

浏览器访问 http://服务器IP/info.php 显示PHP配置信息即为成功

安全增强建议

  • 禁用PHP危险函数:修改php.ini中disable_functions配置项
  • 配置Nginx访问日志与错误日志路径
  • 设置MySQL远程访问权限限制
  • 启用SELinux安全策略:setenforce 1

寰宇互联服务器4核4G云服务器1元/月,网络稳定、抗DDos、国际BGP、性能强劲,十年服务经验QQ:97295700 微信:huanidc

阅读剩余
THE END