LNMP部署及应用

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










posted @ 2024-06-05 01:12  无敌小鲁班  阅读(10)  评论(0编辑  收藏  举报