LNMP架構(gòu)(Linux、Nginx、mysql、php)是構(gòu)建高性能Web應(yīng)用的流行技術(shù)棧。本文將指導(dǎo)您逐步搭建LNMP集群。
一、準(zhǔn)備工作
在開始之前,請確保已完成以下步驟:
-
關(guān)閉防火墻和SELinux: 這對于集群的正常運行至關(guān)重要。執(zhí)行以下命令:
systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config reboot
-
更新系統(tǒng)軟件包: 使用以下命令更新系統(tǒng):
yum update -y
二、安裝nginx
-
安裝依賴: 安裝Nginx所需的依賴庫:
yum install -y pcre-devel zlib-devel openssl-devel
-
下載并解壓Nginx: 下載指定版本的Nginx源碼并解壓到 /usr/local/src/ 目錄:
wget http://nginx.org/download/nginx-1.15.8.tar.gz tar -zxvf nginx-1.15.8.tar.gz -C /usr/local/src/ cd /usr/local/src/nginx-1.15.8
-
配置和編譯安裝: 使用以下命令配置并編譯安裝Nginx,并指定安裝路徑及相關(guān)參數(shù):
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-threads --with-stream --with-stream_ssl_module make && make install
-
創(chuàng)建Nginx用戶: 創(chuàng)建并設(shè)置Nginx運行用戶和用戶組:
useradd nginx chown -R nginx:nginx /usr/local/nginx
-
啟動并設(shè)置開機自啟動: 啟動Nginx服務(wù)并將其設(shè)置為開機自啟動:
systemctl start nginx systemctl enable nginx
三、安裝mysql
-
安裝MariaDB: 安裝mariadb數(shù)據(jù)庫服務(wù)器:
yum install -y mariadb-server mariadb systemctl start mariadb systemctl enable mariadb
-
安全配置: 執(zhí)行MySQL安全配置向?qū)В?/p>
mysql_secure_installation
四、安裝PHP
-
安裝PHP和擴展: 安裝PHP及其必要的擴展:
yum install -y php php-fpm php-mysql php-gd
-
配置PHP-FPM: 修改PHP-FPM配置文件 /etc/php-fpm.d/www.conf,將 user 和 group 修改為 nginx。
-
啟動并設(shè)置開機自啟動: 啟動PHP-FPM服務(wù)并將其設(shè)置為開機自啟動:
systemctl start php-fpm systemctl enable php-fpm
-
配置Nginx: 編輯 /etc/nginx/conf.d/default.conf 文件,在 location ~ .php$ 塊中添加以下配置:
fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params;
-
重啟Nginx: 重啟Nginx服務(wù)使配置生效:
systemctl restart nginx
五、驗證安裝
-
創(chuàng)建測試文件: 創(chuàng)建一個PHP測試文件:
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
-
訪問測試頁面: 在瀏覽器中訪問服務(wù)器公網(wǎng)IP地址/info.php,如果顯示PHP信息頁面,則表示LNMP集群搭建成功。
注意: 以上步驟僅為LNMP集群搭建的基本流程,生產(chǎn)環(huán)境中需要考慮負載均衡、高可用性、安全加固等更多因素。 請根據(jù)實際情況進行調(diào)整和優(yōu)化。