LNMP部署


环境说明

用户IP服务
Nginx192.168.40.99nginx
Mysql192.168.40.100mysql
Php192.168.40.101php

关闭三台防火墙和selinux

# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@nginx ~]# getenforce 
Disabled

配置yum源配置yum源

cd /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

源码部署nginx

创建nginx服务用户

[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
[root@nginx ~]# id nginx 
uid=975(nginx) gid=974(nginx) 组=974(nginx)

安装依赖环境

[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@nginx ~]# dnf -y groups mark install 'Development Tools'
上次元数据过期检查:0:13:17 前,执行于 2022年10月09日 星期日 23时39分57秒。
依赖关系解决。
=============================================================================================================
 软件包                   架构                    版本                        仓库                      大小
=============================================================================================================
安装组:
 Development Tools                                                                                          

事务概要
=============================================================================================================

完毕!

创建日志存放目录

[root@nginx ~]# cd /var/log/
[root@nginx log]# mkdir nginx
[root@nginx log]# chown -R nginx.nginx /var/log/nginx/
[root@nginx log]# ll /var/log/ | grep nginx
drwxr-xr-x  2 nginx  nginx        6 10月  9 23:54 nginx

安装nginx

[root@nginx ~]# cd /usr/local/
[root@nginx local]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-09 23:59:20--  http://nginx.org/download/nginx-1.22.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1073322 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.22.0.tar.gz”

nginx-1.22.0.tar.gz         100%[========================================>]   1.02M   401KB/s  用时 2.6s    

2022-10-09 23:59:24 (401 KB/s) - 已保存 “nginx-1.22.0.tar.gz” [1073322/1073322])
[root@nginx local]# tar xf nginx-1.22.0.tar.gz -C /usr/local/src/
[root@nginx local]# cd /usr/local/src/
[root@nginx src]# ls
nginx-1.22.0
[root@nginx src]# cd nginx-1.22.0/
[root@nginx nginx-1.22.0]# ./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-debug \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_image_filter_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module \
 --http-log-path=/var/log/nginx/access.log \
 --error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

配置环境变量

[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile
[root@nginx ~]# . /etc/profile

服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload

启动服务

[root@nginx ~]# nginx 
[root@nginx ~]# ss -antlp
State         Recv-Q        Send-Q               Local Address:Port                 Peer Address:Port        Process                                                                                                      
LISTEN        0             128                        0.0.0.0:111                       0.0.0.0:*            users:(("rpcbind",pid=904,fd=4),("systemd",pid=1,fd=35))                                                    
LISTEN        0             128                        0.0.0.0:80                        0.0.0.0:*            users:(("nginx",pid=25434,fd=9),("nginx",pid=25433,fd=9))                                                   
LISTEN        0             32                   192.168.122.1:53                        0.0.0.0:*            users:(("dnsmasq",pid=1790,fd=6))                                                                           
LISTEN        0             128                        0.0.0.0:22                        0.0.0.0:*            users:(("sshd",pid=983,fd=4))                                                                               
LISTEN        0             5                        127.0.0.1:631                       0.0.0.0:*            users:(("cupsd",pid=1132,fd=10))                                                                            
LISTEN        0             128                           [::]:111                          [::]:*            users:(("rpcbind",pid=904,fd=6),("systemd",pid=1,fd=37))                                                    
LISTEN        0             128                           [::]:22                           [::]:*            users:(("sshd",pid=983,fd=6))                                                                               
LISTEN        0             5                            [::1]:631                          [::]:*            users:(("cupsd",pid=1132,fd=9))                                                                             

部署Mysql

创建mysql服务用户

[root@mysql ~]# useradd -r -M -s /sbin/nologin mysql
[root@mysql ~]# id mysql 
uid=975(mysql) gid=974(mysql) 组=974(mysql)

安装依赖包

[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

安装MySQL

[root@mysql ~]# rz -E
rz waiting to receive.
[root@mysql ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
模板  图片  下载  桌面  initial-setup-ks.cfg
[root@mysql ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.37-linux-glibc2.12-x86_64  sbin  share  src
[root@mysql local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# ls
bin  games    lib    libexec  mysql-5.7.37-linux-glibc2.12-x86_64  share
etc  include  lib64  mysql    sbin                                 src
[root@mysql local]# chown mysql.mysql mysql*

创建数据库存放目录,配置环境变量

[root@mysql local]# mkdir /opt/data
[root@mysql local]# chown mysql.mysql /opt/data/
[root@mysql local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile
[root@mysql local]# . /etc/profile

初始化安装

[root@mysql local]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T03:57:29.626560Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T03:57:29.818087Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T03:57:29.853255Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T03:57:29.915578Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d3c2ce07-4918-11ed-841d-000c296fdbfa.
2022-10-11T03:57:29.917227Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T03:57:30.574865Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:57:30.574906Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:57:30.575451Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T03:57:30.899485Z 1 [Note] A temporary password is generated for root@localhost: :fw9D70.;mqg
[root@mysql local]# echo "fw9D70.;mqg" > pass
[root@mysql local]# cat pass 
fw9D70.;mqg

配置mysql主配置文件

[root@mysql local]# vim /etc/my.cnf
[root@mysql local]# vim /etc/my.cnf
[root@mysql local]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

配置服务启动脚本

[root@mysql local]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
[root@mysql local]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=mysql server daemon
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
[root@mysql local]# systemctl daemon-reload 
[root@mysql local]# systemctl start mysql.service 
[root@mysql local]# systemctl enable mysql.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /usr/lib/systemd/system/mysql.service.
[root@mysql local]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:111                 0.0.0.0:*                    
LISTEN     0           32               192.168.122.1:53                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           5                    127.0.0.1:631                 0.0.0.0:*                    
LISTEN     0           80                           *:3306                      *:*                    
LISTEN     0           128                       [::]:111                    [::]:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
LISTEN     0           5                        [::1]:631                    [::]:*                    
[root@mysql local]# mysql -uroot -p"fw9D70.;mqg"
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

需要安装依赖包
[root@mysql local]# dnf whatprovides libncurses.so.5
上次元数据过期检查:0:04:22 前,执行于 2022年10月11日 星期二 00时10分09秒。
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
仓库        :base
匹配来源:
提供    : libncurses.so.5

[root@mysql local]# dnf -y install ncurses-compat-libs
登录数据库,重新设置密码
[root@mysql ~]# mysql -uroot -pfw9D70.;mqg
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

安装后配置

[root@mysql ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@mysql ~]# vim /etc/man_db.conf 
[root@mysql ~]# cd /etc/ld.so.conf.d/
[root@mysql ld.so.conf.d]# ls
kernel-4.18.0-358.el8.x86_64.conf  libiscsi-x86_64.conf
[root@mysql ld.so.conf.d]# echo "/usr/local/mysql/lib" > mysql.conf
[root@mysql ld.so.conf.d]# ls
kernel-4.18.0-358.el8.x86_64.conf  libiscsi-x86_64.conf  mysql.conf

源码安装php

安装依赖包和工具

[root@php local]# dnf -y install make libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd --nobest
[root@php local]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

安装php

[root@php ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       php-7.1.10.tar.gz
模板  图片  下载  桌面  initial-setup-ks.cfg
[root@php ~]# tar xf php-7.1.10.tar.gz -C /usr/local/
[root@php ~]# cd /usr/local/
[root@php local]# ls
bin  etc  games  include  lib  lib64  libexec  php-7.1.10  sbin  share  src
[root@php local]# cd php-7.1.10/
[root@php php-7.1.10]#  ./configure --prefix=/usr/local/php7  \
 --with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
 --disable-rpath \
 --enable-shared \
 --enable-soap \
 --with-openssl \
 --enable-bcmath \
 --with-iconv \
 --with-bz2 \
 --enable-calendar \
 --with-curl \
 --enable-exif  \
 --enable-ftp \
--enable-gd \
 --with-jpeg \
 --with-zlib-dir \
 --with-freetype \
 --with-gettext \
 --enable-json \
 --enable-mbstring \
 --enable-pdo \
 --with-mysqli=mysqlnd \
 --with-pdo-mysql=mysqlnd \
 --with-readline \
 --enable-shmop \
 --enable-simplexml \
 --enable-sockets \
 --with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
 --enable-pcntl \
--enable-posix
[root@php php-7.1.10]# make -j4 && make install

安装后配置

[root@php local]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@php local]# ls
bin  etc  games  include  lib  lib64  libexec  php7  php-7.1.10  sbin  share  src
[root@php local]# cd php7
[root@php php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@php php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@php php7]# ldconfig
[root@php php7]# source /etc/profile.d/php7.sh

配置php-fpm

[root@php local]# cd php-7.1.10/
[root@php php-7.1.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@php php-7.1.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-7.1.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@php php-7.1.10]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@php php-7.1.10]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

启动php-fpm

[root@php php-7.1.10]# cd /usr/lib/systemd/system
[root@php system]# vim php-fpm.service

[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm  stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@php system]# systemctl daemon-reload 
[root@php system]# systemctl restart php-fpm.service 
[root@php system]# systemctl enable php-fpm.service 
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

配置php

[root@php system]# cd /usr/local/php7/etc/
[root@php etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@php etc]# cd php-fpm.d/
[root@php php-fpm.d]# ls
www.conf  www.conf.default
[root@php php-fpm.d]# vim www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.100.10

生成测试页面

[root@php php-fpm.d]# cd /var/www/html/
[root@php html]# ls
index.html
[root@php html]# vim index.php
<?php
  phpinfo();
?>
[root@php html]# chown -R apache.apache /var/www/html/
[root@php html]# systemctl restart php-fpm.service 
[root@php html]# systemctl enable php-fpm.service

在nginx节点操作,整合nginx和php

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root       /var/www/html;
            fastcgi_pass   192.168.40.101:9000;  
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

测试nginx配置文件

[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s stop
[root@nginx ~]# nginx   

测试
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值