LNMP部署及應用

无敌小鲁班發表於2024-06-05

目錄
  • 1.LNMP工作原理
  • 2.LNMP安裝
    • (1)安裝 Nginx 服務
    • (2)安裝MySQL服務
    • (3)安裝配置 PHP解析環境
    • (4)驗證
    • (5)安裝論壇
    • (6)客網站應用

1.LNMP工作原理

2.LNMP安裝

nginx、mysql、php關閉防火牆

(1)安裝 Nginx 服務

安裝 Nginx 服務20.0.0.100

cd /opt/
rz -E  #上傳
tar xf nginx-1.26.0.tar.gz
cd nginx-1.26.0/
##安裝依賴包
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make  
##建立執行使用者、組
useradd -M -s /sbin/nologin nginx   ##建立執行使用者
cat /etc/passwd | grep nginx:

 ##編譯安裝
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module  --with-http_ssl_module

make -j 2 && make install
##路徑最佳化
cd /usr/local/nginx/
ls sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cd /usr/lib/systemd/system

##新增 Nginx 系統服務
vim nginx.service
[Unit]   
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl daemon-reload   ##過載
systemctl start nginx   ##開啟
systemctl enable nginx
netstat -lntp | grep nginx






(2)安裝MySQL服務

安裝 mysql 服務20.0.0.110

cd /opt/
rz -E  #上傳
tar xf mysql-boost-5.7.44.tar.gz
cd mysql-5.7.44/

#安裝Mysql環境依賴包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake openssl-devel    

useradd -M -s /sbin/nologin mysql   ##建立執行使用者
cat /etc/passwd | grep mysql:

 
#配置軟體模組
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

##編譯安裝
make -j 2 && make install
##清除mariadb快取
yum remove -y mariadb*


#修改mysql配置檔案
vim /etc/my.cnf

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES


#更換mysql配置檔案和安裝目錄的屬主屬組
chown mysql:mysql /etc/my.cnf
 ll !$
 ## ll /etc/my.cnf            
cd /usr/local/
chown -R mysql:mysql mysql/
ll
#在環境變數中新增mysql的bin和lib目錄
cd mysql/bin/
pwd ---- /usr/local/mysql/bin/

vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

source /etc/profile    #載入        
echo $PATH



#mysql初始化操作
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

#新增mysqld系統服務
cd /usr/local/mysql/usr/lib/systemd/system/
ls
cp mysqld.service /usr/lib/systemd/system
systemctl daemon-reload   ##過載
systemctl enable mysqld
systemctl start mysqld   ##開啟
netstat -lntp | grep mysqld

#修改mysql密碼

mysqladmin -u root -p password "abc23"
回車
mysql -u root -pabc123
show databases;
quit

##驗證mysql測試頁http://www.xlb666.com/mysql.php
mysql -u root -pabc123
create database discuz;
show databases;
grant all on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
quit


##論壇資料庫http://www.xlb666.com/discuz/index.php
mysql -u root -pabc123
show databases;
create database discuz;
show databases;
grant all on discuz.* to 'xlb'@'%' identified by 'xlb666';
flush privileges;

drop database discuz;  ##刪除
















(3)安裝配置 PHP解析環境

安裝php服務20.0.0.130
cd /opt/
rz -E  #上傳
tar xf php-7.1.10.tar.bz2
ls
cd php-7.1.10/

#安裝環境依賴包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel pcre-devel

#配置軟體模組
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

##編譯安裝
make -j 2 && make install
##路徑最佳化
cd /usr/local/php/
ls
ls bin/
ls sbin/
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

php --ini
##修改主配置檔案
cd /opt/php-7.1.10/
ls
cp php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/lib/
ls
vim php.ini
--939行--取消註釋,修改
date.timezone = Asia/Shanghai

#修改程序服務配置檔案
cd /usr/local/php/
cd etc/
pwd ----- /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
ls
vim php-fpm.conf
17取消pid = run/php-fpm.pid    ##啟動php-fpm

#修改擴充套件配置檔案
cd php-fpm.d/
ls
cp www.conf.default www.conf    ##調整擴充套件配置檔案:

vim www.conf
#修改使用者名稱和組名23/24行
user = nginx  
group = nginx  
#將監聽地址改為本機地址36行
listen = 20.0.0.130:9000
listen.allowed_clients = 20.0.0.100  ##允許訪問nginx62行

#建立使用者
useradd -M -s /sbin/nologin nginx
cat /etc/passwd | grep nginx:   安裝配置 PHP解析環境20.0.0.130
#啟動php-fpm
cd /opt/php-7.1.10/
ls
cd sapi/
ls
cd fpm/
ls             
cp php-fpm.service /usr/lib/systemd/system
systemctl daemon-reload
systemctl start php-fpm.service
systemctl enable php-fpm.service
netstat -lntp | grep php

systemctl restart php-fpm.service   ##重新啟動服務














啟動php-fpm

(4)驗證

nginx
##修改 nginx.conf 配置檔案
cd /usr/local/nginx/conf/

vim nginx.conf
     worker_processes auto;
     worker_rlimit_nofile 65535;
   
   events {
     use epoll;  ##新增
     worker_connections 65535;
    
    server_name www.xlb666.com;
    charset utf-8;

       
       location / {      
            root   /var/www/html; 
          index  index.php index.html index.htm index.php;
 

  location ~ \.php$ {
  root /var/www/html;   
  fastcgi_pass 20.0.0.130:9000;  ##php地址
  fastcgi_index index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;	
  include fastcgi_params;

}

nginx -t
##驗證PHP 測試頁

##為虛擬主機準備網頁文件
mkdir -p /var/www/html
 ##動態頁面  
cd /var/www/html/               
vim test.php
<?php
phpinfo();
?>

##靜態頁面
cd /usr/local/nginx/html/  

vim xlb.html
<h1>this is xlb static web</h1>
<img src="xlb.jpg"/>

echo "20.0.0.100 www.xlb666.com" >> /etc/hosts

http://www.xlb666.com/xlb.html
http://20.0.0.100/xlb.html

yum install -y rpcbind nfs-utils

vim /etc/exports
/var/www/html 20.0.0.0/24(rw,sync,no_root_squash)

systemctl enable --now rpcbind 
systemctl enable --now nfs
showmount -e










因為我們靜態頁面是由Nginx直接處理和響應的,而動態頁面是Nginx透過fastcgi介面轉發給php-fpm程序接受,然後再由php解析器處理解釋動態頁面後響應返回給Nginx,Nginx再返回給客戶端的,所以我們要在將動態頁面位置也放入到php伺服器中

php
showmount -e 20.0.0.100

vim /etc/fstab
20.0.0.100:/var/www/html  /var/www/html   nfs  defaults 0 0

mkdir -p /var/www/html
mount -a
df -h
ls /var/www/html/

http://www.xlb666.com/test.php
systemctl restart nginx.service   ##重啟nginx服務






(5)安裝論壇

***Nginx***
#安裝論壇
cd /opt/
上傳
Discuz_X3.4_SC_UTF8.zip
unzip Discuz_X3.4_SC_UTF8.zip ##解壓
cd dir_SC_UTF8/
cp -r upload/ /var/www/html/discuz
cd /var/www/html/
ls
chown -R nginx:nginx discuz/


***php***
cd /var/www/html/
ll
chown -R nginx:nginx discuz/
ll
systemctl restart php-fpm.service
systemctl restart nginx.service 

http://www.xlb666.com/discuz/index.php







(6)客網站應用

***nginx***

cd /opt/
上傳
nginx-1.26.0.tar.gz

##解壓
tar xf wordpress-4.9.4-zh_CN.tar.gz
ls
cp -a wordpress /var/www/html/
cd /var/www/html/
chown -R nginx wordpress/
chown -R nginx:nginx wordpress/

***php***
cd /var/www/html/
chown -R nginx wordpress/
chown -R nginx:nginx wordpress/


***mysql***
mysql -u root -pabc123
create database wordpress;
show databases;
grant all on wordpress.* to 'xlb'@'%' identified by 'xlb666';
flush privileges;

http://www.xlb666.com/wordpress










相關文章