分离部署lnmp

环境:

主机名 IP 安装的服务
nginx 192.168.44.128 nginx
mysql 192.168.44.129 mysql
php 192.168.44.130 php

安装nginx

//关闭防火墙与SELINUX
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# setenforce 0

//安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@nginx ~]# yum -y groups mark install 'Development Tools'
 
//下载nginx包
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.0.tar.gz
 
//创建nginx系统用户
[root@nginx ~]# useradd  -r -M -s /sbin/nologin nginx
 
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
 
//编译安装nginx
[root@nginx ~]# tar -xf nginx-1.20.0.tar.gz
[root@nginx ~]# cd nginx-1.20.0
[root@nginx nginx-1.20.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.20.0]# make && make install
 
//配置环境变量
[root@nginx nginx-1.20.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx nginx-1.20.0]# . /etc/profile.d/nginx.sh
 
 
//修改配置文件
[root@nginx ~]# vim  /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            #添加index.php
            index index.php  index.html index.htm;
        }
 
        location ~ \.php$ {
          # 设置php主机IP的监听端口
            fastcgi_pass   192.168.44.130:9000;
           # 设置nginx的默认首页文件
            fastcgi_index  index.php;
          # 设置脚本文件请求的路径
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;     #将$scripts修改为php根网站目录
         #引入fastcgi的配置文件
            include        fastcgi_params;
        }
 
//检查语法是否有误
[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
 
//创建ndex.php
[root@nginx ~]# cat > /usr/local/nginx/html/index.php <<EOF
> <?php
>     phpinfo();
> ?>
> EOF
 
//启动服务
[root@nginx ~]# nginx

安装配置mysql

//关闭防火墙与SELINUX
[root@mysql ~]# systemctl disable --now firewalld
[root@mysql ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@mysql ~]# setenforce 0

//下载mysql包
[root@mysql ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
 
//安装依赖环境
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
 
//创建用户
[root@mysql ~]# useradd  -r -M -s /sbin/nologin  mysql
 
//安装mysql
[root@mysql ~]#  tar  -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# ln -sv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/  /usr/local/mysql
[root@mysql ~]# cd /usr/local/
[root@mysql local]# chown -R mysql.mysql mysql*
 
//添加环境变量
[root@mysql local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh
[root@mysql local]# source /etc/profile.d/myslq.sh
 
[root@mysql local]# ln -s /usr/local/mysql/include/  /usr/include/mysql
 
[root@mysql local]# echo '/usr/local/mysql/lib' >/etc/ld.so.conf.d/mysql.conf
[root@mysql local]# ldconfig
 
//创建数据存放目录
[root@mysql local]# mkdir /opt/mydata
[root@mysql local]# chown  -R mysql.mysql /opt/mydata/
 
//初始化数据库
[root@mysql local]# mysqld --initialize-insecure --user=mysql --datadir=/opt/mydata
 
//生成配置文件
[root@mysql local]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/mydata
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/mydata/mysql.pid
user=mysql
skip-name-resolve
EOF
 
//配置服务启动脚本
[root@mysql local]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@mysql local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@mysql local]# sed -ri 's#^(datadir=).*#\1/opt/mydata#g' /etc/init.d/mysqld
 
//启动MySQL
[root@mysql local]# service mysqld start
 
//设置密码
[root@mysql local]# mysql -e "set password = password('123456')"

安装php

//关闭防火墙与SELINUX
[root@php ~]# systemctl disable --now firewalld
[root@php ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@php ~]# setenforce 0

//配置yum源
[root@php ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.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 ~]# sed -i 's#\$releasever#8#g' /etc/yum.repos.d/CentOS-Base.repo
[root@php ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@php ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@php ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@php ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel*
[root@php ~]# yum clean all
[root@php ~]# yum makeache


//安装配置php
[root@php ~]# 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
[root@php ~]# yum -y install php*
[root@php ~]# vim /etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock    #注释此行
listen = 0.0.0.0:9000  #添加监听端口


; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 192.168.44.128  #修改成nginx主机的ip
 
[root@php ~]# cat /var/www/html/index.php
<?php
    phpinfo();
?>
 
[root@php ~]# chown -R nginx.nginx /var/www/html/
[root@php ~]# systemctl  start php-fpm

在浏览器*问nginx的IP测试

分离部署lnmp

 

上一篇:LNMP架构的源码编译


下一篇:linux学习——华农决战Linux(1)IDEA导出项目参考