LNMP部署

LNMP部署

环境

系统ip部署服务
centos8-stream192.168.245.128nginx
centos8-stream192.168.245.131php
centos8-stream192.168.245.129mysql

lnmp由linux,nginx,mysql,php构成

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。

作为负载均衡服务器:Nginx 既可以在内部直接支持RailsPHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。

作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。具体的优点或者配置文件详解可以看之前的nginx基础

#关闭三台服务器防火墙
[root@localhost nginx-1.20.2]# systemctl stop firewalld.service 
[root@localhost nginx-1.20.2]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost nginx-1.20.2]# setenforce 0

部署nginx:

#创建系统用户nginx
[root@stream2 ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖
[root@stream2 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@stream2 ~]# yum -y groups mark install 'Development Tools'
#存放目录创建
[root@stream2 ~]# mkdir -p /var/log/nginx
[root@stream2 ~]# chown -R nginx.nginx /var/log/nginx
#下载源码包
[root@stream2 src]# pwd
/usr/src
[root@stream2 src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@stream2 src]# ls
debug  kernels  nginx-1.22.0.tar.gz
#编译安装
[root@stream2 src]# tar xf nginx-1.22.0.tar.gz 
[root@stream2 src]# cd nginx-1.22.0/
[root@stream2 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

#nginx配置,服务控制方式,使用nginx命令
[root@stream2 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@stream2 nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@stream2 nginx-1.22.0]# source /etc/profile.d/nginx.sh 
[root@stream2 nginx-1.22.0]# which nginx 
/usr/local/nginx/sbin/nginx
#使用 nginx -t 检查配置文件语法
[root@stream2 nginx-1.22.0]# 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@stream2 nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0

#写service文件
[root@stream2 nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@stream2 nginx-1.22.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx  server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@stream2 nginx-1.22.0]# systemctl daemon-reload 
[root@stream2 nginx-1.22.0]# systemctl restart nginx.service 
[root@stream2 nginx-1.22.0]# systemctl enable nginx.service 
--
[root@stream2 nginx-1.22.0]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           128                       [::]:22                     [::]:*    

部署php

#下载源码包
[root@stream2 nginx-1.22.0]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
#安装依赖
[root@stream2 nginx-1.22.0]# dnf -y install sqlite-devel make wget 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 --allowerasing oniguruma  --skip-broken --nobest
[root@stream2 nginx-1.22.0]# yum -y install libsqlite3x-devel libxml2-devel libzip-devel
[root@stream2 nginx-1.22.0]# dnf --enablerepo=powertools install oniguruma-devel

#解压缩包,然后开始编译安装
[root@stream2 nginx-1.22.0]# tar -xf php-8.1.11.tar.gz 
[root@stream2 nginx-1.22.0]# cd php-8.1.11/
[root@stream2 nginx-1.22.0]# ./configure --prefix=/usr/local/php8 \
 --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@stream2 php-8.1.11]# make install
#环境变量
[root@stream2 php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@stream2 php-8.1.11]# source /etc/profile.d/php8.sh
[root@stream2 php-8.1.11]# which php
/usr/local/php8/bin/php
[root@stream2 php-8.1.11]# php -v
PHP 8.1.11 (cli) (built: Oct 19 2022 13:47:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies

#配置php-fpm
[root@stream2 php-8.1.11]# cp php.ini-production  /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@stream2 php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@stream2 php-8.1.11]# chmod +x /etc/init.d/php-fpm
[root@stream2 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@stream2 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

#编写service文件启动服务并设为开机自启
[root@stream2 php-8.1.11]# cat > /usr/lib/systemd/system/php.service <<EOF
 [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
EOF
#设置开机自启
[root@stream2 php-8.1.11]# systemctl daemon-reload
[root@stream2 php-8.1.11]# systemctl restart php.service 
[root@stream2 php-8.1.11]# systemctl enable php.service 
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@stream2 php-8.1.11]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*                      
LISTEN      0           128                       [::]:22                     [::]:*       


[root@localhost php-fpm.d]# vim /usr/local/php8/etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000  #找到这一行 127.0.0.1修改为自己的ip
;listen.allowed_clients = 192.168.245.128 #允许访问ip

[root@localhost ~]# mkdir -p /usr/local/nginx/html
[root@localhost ~]#  groupadd -r -g 955 nginx
[root@localhost ~]# useradd -M -s /sbin/nologin -g 955 -u 955 nginx
[root@localhost ~]# chown -R nginx.nginx /usr/local/nginx/html/
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
<?php
    phpinfo();
?>

[root@localhost php-fpm.d]# chkconfig --add php-fpm

部署mysql

 #安装依赖包
 [root@master ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
 #下载二进制格式的mysql软件包:https://downloads.mysql.com/archives/community/
 网页访问上地址下载
 ​
 [root@master ~]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
 [root@master ~]# cd /usr/local/
 [root@master local]# ls
 apache     apr       bin  games  include  lib64    mysql-5.7.38-linux-glibc2.12-x86_64  share
 apache2.4  apr-util  etc  httpd  lib      libexec  sbin                                 src
 [root@master local]# mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
 [root@master local]# 
 #修改属组
 [root@master local]# ls
 apache     apr       bin  games  include  lib64    mysql  share
 apache2.4  apr-util  etc  httpd  lib      libexec  sbin   src
 [root@master local]# chown -R mysql.mysql /usr/local/mysql
 [root@master local]# ll /usr/local/mysql -d
 drwxr-xr-x. 9 mysql mysql 129 Aug  2 21:03 /usr/local/mysql
 [root@master local]# #添加环境变量
 [root@master ~]# ls /usr/local/mysql/
 LICENSE  README  bin  docs  include  lib  man  share  support-files
 [root@master ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
 [root@master ~]# . /etc/profile.d/mysql.sh
 [root@master ~]# echo $PATH
 /usr/local/mysql/bin:/usr/local/httpd/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
 [root@master ~]# #建立数据存放目录
 [root@master ~]# mkdir /opt/data
 [root@master ~]# chown -R mysql.mysql /opt/data/
 [root@master ~]#  ll /opt/
 total 468
 drwxr-xr-x. 2 mysql mysql      6 Aug  2 21:06 data
 -rw-r--r--. 1 root  root  478044 Aug  2 10:55 tang.sql
 [root@master ~]# 
 ​
 ​
 #初始化数据库
 [root@master ~]#  /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
 ------------------
  root@localhost: s!a<7i5!DoLa
 #这里有随机生成的密码需要记住
 [root@master ~]# echo 's!a<7i5!DoLa' >> passwrd
 [root@master ~]# cat passwrd 
 s!a<7i5!DoLa
 [root@master ~]# #配置mysql
 [root@master ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
 '/usr/local/include/mysql' -> '/usr/local/mysql/include/'
 [root@master ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
 [root@master ~]# ldconfig
 [root@master ~]# #生成配置文件
 [root@master ~]# cat > /etc/my.cnf <<EOF
 > [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
 > EOF
 [root@master ~]# #配置服务启动脚本
 [root@master ~]#  cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
 [root@master ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
 [root@master ~]#  sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
 [root@master ~]# #启动mysql
 [root@master ~]# service mysqld start
 Starting MySQL.Logging to '/opt/data/master.err'.
  SUCCESS! 
 [root@master ~]# ss -antl
 State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    Process    
 LISTEN     0          128                  0.0.0.0:22                0.0.0.0:*                  
 LISTEN     0          80                         *:3306                    *:*                  
 LISTEN     0          128                     [::]:22                   [::]:*                  
 [root@master ~]# #修改密码
 #使用临时密码登录
 [root@master ~]# mysql -uroot -p's!a<7i5!DoLa'
 ​
 mysql> set password = password('1');
 Query OK, 0 rows affected, 1 warning (0.00 sec)
 ​
 mysql> 
————————————————

修改nginx配置文件

[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# cd conf/
[root@localhost conf]# vim nginx.conf
       location / {
           root   html;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.245.131:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
                }


[root@localhost conf]# cd ..
[root@localhost nginx]# cd html/
[root@localhost html]# vim index.html 
[root@localhost html]# cat index.html
<?php phpinfo(); ?>

image-20221011170535749

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值