部署LNMP架构

部署LNMP架构


环境说明

主机名IP地址服务系统
nginx192.168.188.128nginxcentos8
mysql192.168.188.129mysqlcentos8
php192.168.188.137phpcentos8

在三台机器上分别部署好Nginx,MySQL,PHP

👉nginx安装文档
👉mysql安装文档

👉php官网

php 安装

#关闭selinux和防火墙
[root@php ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@php ~]# 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@php ~]# setenforce 0
[root@php ~]# getenforce
Disabled

#配置yum源
[root@php ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@php ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

#安装依赖包
[root@php ~]# dnf -y install epel-release
[root@php ~]# dnf -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 wget gcc gcc-c++ make
[root@php ~]# 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 ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@php ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/src/
[root@php ~]# cd /usr/local/src/php-8.1.11/
[root@php php-8.1.11]# 

#编译安装php
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php \
 --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@php php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@php php-8.1.11]# cd /usr/local/php/
[root@php php]# ls
bin  etc  include  lib  php  sbin  var

#配置服务(复制默认配置文件,使配置文件生效)
[root@php php]# cp etc/php-fpm.conf.default etc/php-fpm.conf 
[root@php php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
#头文件映射
[root@php php]# ln -s /usr/local/php /usr/include/php
#创建库文件
[root@php ~]# echo "/usr/local/php/lib" > /etc/ld.so.conf.d/php.conf
[root@php ~]# ldconfig

#编写service文件
[root@php ~]# cat > /usr/lib/systemd/system/php.service << EOF
[Unit]
Description=php server daemon
After=network.target 
  
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill 
ExecReload=/bin/kill -HUP $MAINPID
  
[Install]
WantedBy=multi-user.target
EOF

[root@php ~]# systemctl daemon-reload
[root@php ~]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@php ~]# ss -anlt
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          128                127.0.0.1:9000              0.0.0.0:*
LISTEN     0          128                     [::]:22                   [::]:*
[root@php ~]#

连接nginx和php

php端

#编写测试主页,这里的目录对应的是nginx存放主页的目录,也可以自定义目录
[root@php ~]# mkdir -p /usr/local/nginx/html
cat > /usr/local/nginx/html/index.php << EOF
<?php
   phpinfo();
?>
EOF

#修改配置文件
[root@php ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
listen = 192.168.188.137:9000  //php服务监听的端口,(也就是服务开放端口)
;listen.allowed_clients = 192.168.188.128  //nginx端ip,(允许访问的ip)

#重启服务生效配置
[root@php ~]# systemctl restart php.service

nginx端

#编写测试主页
[root@nginx ~]# cat > /usr/local/nginx/html/index.php << EOF
<?php
   phpinfo();
?>
EOF
[root@nginx ~]# systemctl restart nginx.service

#nginx端编辑配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
 …………
 location / {
            root   html;
            index  index.html index.htm index.php;     //这里加上index.php访问页
        }
……  //下面的配置需要取消注释
        location ~ \.php$ {				//匹配以.php结尾的文件
            root           html;			//在/usr/local/nginx/html目录下
            fastcgi_pass   192.168.188.137:9000;	//php的ip地址和端口
            fastcgi_index  index.php;			
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;		//这里的$document_root是匹配root指定的路径
            include        fastcgi_params;
        }
……

#重新加载服务配置
[root@nginx ~]# systemctl reload nginx.service

测试访问

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随便投投

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值