LNMP部署

LNMP部署

    • 1.准备工作
    • 2.nginx安装
    • 3.mysql安装
    • 4.php安装
    • 5.修改配置文件

1.准备工作

//关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
[root@localhost ~]# setenforce 0

2.nginx安装

//创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@localhost ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx: /var/log/nginx

//下载nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.21.3.tar.gz
--2021-10-25 23:25:56--  http://nginx.org/download/nginx-1.21.3.tar.gz
正在解析主机 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
正在连接 nginx.org (nginx.org)|52.58.199.22|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1066609 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.21.3.tar.gz”

nginx-1.21.3.tar.gz       100%[=====================================>]   1.02M  28.3KB/s  用时 32s     

2021-10-25 23:26:30 (32.7 KB/s) - 已保存 “nginx-1.21.3.tar.gz” [1066609/1066609])

//编译安装
[root@localhost ~]# ls 
anaconda-ks.cfg  nginx-1.21.3.tar.gz
[root@localhost ~]# tar xf nginx-1.21.3.tar.gz 
[root@localhost ~]# cd nginx-1.21.3/
[root@localhost nginx-1.21.3]# ./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@localhost nginx-1.21.3]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# which nginx		//成功找到nginx
/usr/local/nginx/sbin/nginx

//启动nginx,无需-s指定
[root@localhost ~]# nginx 
[root@localhost ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*         
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         

3.mysql安装

//安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

//创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql

//下载二进制包
[root@localhost src]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//解压、重命名目录及修改属主和属组
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost local]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
apache  apr  apr-util  bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@localhost local]# chown -R mysql: mysql/

//添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql		//查看能否找到mysql命令
/usr/local/mysql/bin/mysql

//指定帮助文档位置、给头文件创建软链接
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql

//指定lib库文件位置
[root@localhost ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig 	//读取,生效

//创建数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql: /opt/data/

//初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data

//生成配置文件
[root@localhost ~]# cat /etc/my.cnf
[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@localhost ~]# sed -ri 's#(^basedir=$)#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# sed -ri 's#(^datadir=$)#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service 
[Unit]
Description=mysql server daemon
After=network.target 

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

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enable --now mysqld.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           80                           *:3306                      *:*         
tcp        LISTEN      0           128                          *:80                        *:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         

//登录数据库设置密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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('123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)

4.php安装

//安装epel-release源
[root@localhost ~]# yum -y install epel-release
//安装依赖包
[root@localhost ~]# yum -y install 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 libsqlite3x-devel libzip-devel

//下载php并解压
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-8.0.11.tar.xz
[root@localhost src]# tar xf php-8.0.10.tar.xz

//安装oniguruma-devel库
[root@localhost src]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

//编译安装
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--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-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@localhost php-8.0.10]# make && make install

//添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
[root@localhost php-8.0.10]# which php		
/usr/local/php8/bin/php
[root@localhost php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Sep 24 2021 18:26:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies

//配置php-fpm
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini 
cp:是否覆盖'/etc/php.ini'? y
[root@localhost php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm	//启动的控制脚本
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@localhost ~]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost ~]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

//配置开机自启
[root@localhost ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php server daemon
After=network.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@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enable --now 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.

[root@localhost ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*         
tcp        LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         
tcp        LISTEN      0           80                           *:3306                      *:*         

5.修改配置文件

修改nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 43         location / {
 44             root   html;
 45             index  index.php index.html index.htm;		//添加index.php
 46         }
......
//取消下面的注释,并更改69行
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 70             include        fastcgi_params;
 71         }

//编写php测试页面
[root@localhost ~]# vim /usr/local/nginx/html/index.php
[root@localhost ~]# cat /usr/local/nginx/html/index.php


//重启nginx
[root@localhost ~]# nginx -s stop;nginx
[root@localhost ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*         
tcp        LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         
tcp        LISTEN      0           80                           *:3306                      *:*         

修改php配置文件

[root@localhost ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
22 ;       will be used.
23 user = nginx		//改为nginx用户和组
24 group = nginx

重启所有服务

[root@localhost ~]# nginx -s stop;nginx
[root@localhost ~]# systemctl restart mysqld.service 
[root@localhost ~]# systemctl restart php-fpm.service 
[root@localhost ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*         
tcp        LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         
tcp        LISTEN      0           80                           *:3306                      *:*       

网页访问
LNMP部署_第1张图片

你可能感兴趣的:(nginx,运维,lnmp)