CentOS7.2系統(tǒng)LNMP環(huán)境搭建全流程
環(huán)境準(zhǔn)備與系統(tǒng)更新
執(zhí)行系統(tǒng)更新確保組件兼容性:
yum update -y && reboot
Nginx服務(wù)部署
安裝EPEL擴(kuò)展源
yum install epel-release -y
安裝與啟動(dòng)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數(shù)據(jù)庫安裝
添加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環(huán)境配置
添加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集成
創(chuàng)建測(cè)試站點(diǎn)配置
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
驗(yàn)證配置并重啟服務(wù)
nginx -t
systemctl restart nginx
環(huán)境驗(yàn)證測(cè)試
創(chuàng)建PHP探針文件
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
訪問性測(cè)試
瀏覽器訪問 http://服務(wù)器IP/info.php 顯示PHP配置信息即為成功
安全增強(qiáng)建議
- 禁用PHP危險(xiǎn)函數(shù):修改php.ini中disable_functions配置項(xiàng)
- 配置Nginx訪問日志與錯(cuò)誤日志路徑
- 設(shè)置MySQL遠(yuǎn)程訪問權(quán)限限制
- 啟用SELinux安全策略:
setenforce 1