CentOS安裝LNMP環(huán)境搭建指南
環(huán)境準(zhǔn)備
確保系統(tǒng)為CentOS 7/8,并已通過SSH連接服務(wù)器。執(zhí)行以下命令更新系統(tǒng):
yum update -y
安裝Nginx
添加EPEL倉庫并安裝Nginx:
yum install epel-release -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
訪問服務(wù)器IP驗證Nginx是否運行。
安裝MySQL
通過以下命令安裝MariaDB(MySQL分支):
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
執(zhí)行安全配置腳本:
mysql_secure_installation
安裝PHP
添加Remi倉庫并安裝PHP 7.4:
yum install https://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服務(wù):
systemctl start php-fpm
systemctl enable php-fpm
配置Nginx支持PHP
編輯/etc/nginx/conf.d/default.conf
,在server
塊中添加:
location ~ .php$ {
root /usr/share/nginx/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生效配置:
systemctl restart nginx
驗證PHP解析
創(chuàng)建/usr/share/nginx/html/info.php
文件,寫入:
<?php phpinfo(); ?>
訪問http://服務(wù)器IP/info.php查看PHP信息頁。
防火墻與SELinux設(shè)置
開放HTTP/HTTPS端口:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
若啟用SELinux,執(zhí)行:
setsebool -P httpd_can_network_connect 1
環(huán)境優(yōu)化建議
- 修改
php.ini
調(diào)整內(nèi)存限制與上傳大小 - 配置MySQL的
my.cnf
優(yōu)化性能 - 為Nginx啟用Gzip壓縮與緩存機(jī)制