LNMP分离部署

1 篇文章 0 订阅

环境说明:

服务类型IP地址应用系统平台
nginx192.168.93.129nginxcentos7
redhat7
mysql192.168.93.130mysqlcentos7
redhat7
php192.168.93.131phpcentos7
redhat7

1.nginx服务器配置

1.1 nginx的安装

//创建系统用户nginx
[root@nginx ~]# groupadd -r nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin -g nginx nginx

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

//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
[root@nginx ~]# ll /var/log/nginx/ -d
drwxr-xr-x. 2 nginx nginx 6 8月  26 15:24 /var/log/nginx/

//下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx src]# ls
debug  kernels  nginx-1.16.1.tar.gz
[root@nginx src]# tar xf nginx-1.16.1.tar.gz 
[root@nginx src]# ls
debug    nginx-1.16.1   kernels  nginx-1.16.1.tar.gz
[root@nginx src]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# ./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
//注意:用网络源需要加上--with-http_realip_module \,本地源不需要

[root@nginx nginx-1.16.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
&& make install

1.2 nginx安装后配置

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

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

//启动nginx
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:80                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:9000                   *:*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      80        :::3306                  :::*                  

//服务控制脚本,把nginx添加到chkconfig
[root@nginx ~]# vim nginx.sh
#!/bin/bash

nginx=/usr/local/nginx/sbin/nginx
# chkconfig: 2345  20  80
# description:dsjaaazkxsjdx

case $1 in 
    'start')
        $nginx
        ;;
    'stop')
        $nginx -s stop
        ;;
    'restart')
        $nginx -s stop
        $nginx
        ;;
      *)
        echo 'Usage: service nginx start|stop|restart'
        ;;
esac

[root@nginx ~]# cd /etc/init.d/
[root@nginx ~]# mv nginx.sh /etc/init.d/nginx
[root@nginx ~]# chmod +x /etc/init.d/nginx 
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
nginx          	0:关	1:关	2:开	3:开	4:开	5:开	6:关
rhnsd          	0:关	1:关	2:开	3:开	4:开	5:开	6:关

[root@nginx init.d]# service nginx stop
[root@nginx init.d]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:9000                   *:*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      80        :::3306                  :::*                  

2. mysql服务器配置

//安装依赖包

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

//创建用户和组

[root@mysql ~]# groupadd -r -g 306 mysql
[root@mysql ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下载二进制格式的mysql软件包

[root@mysql ~]# cd /usr/src/
[root@mysql src]# ls
debug  kernels
[root@mysql src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mysql src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mysql src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql src]# cd
[root@mysql ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.22-linux-glibc2.12-x86_64  share
[root@mysql ~]# cd /usr/local/
[root@mysql local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@mysql local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 3月  10 2016 bin
drwxr-xr-x. 2 root root   6 3月  10 2016 etc
drwxr-xr-x. 2 root root   6 3月  10 2016 games
drwxr-xr-x. 2 root root   6 3月  10 2016 include
drwxr-xr-x. 2 root root   6 3月  10 2016 lib
drwxr-xr-x. 2 root root   6 3月  10 2016 lib64
drwxr-xr-x. 2 root root   6 3月  10 2016 libexec
lrwxrwxrwx  1 root root  36 8月  10 20:09 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 8月  10 20:08 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 3月  10 2016 sbin
drwxr-xr-x. 5 root root  49 3月  25 02:56 share
drwxr-xr-x. 2 root root   6 3月  10 2016 src

//修改目录/usr/local/mysql的属主属组

[root@mysql ~]# chown -R mysql.mysql /usr/local/mysql
[root@mysql ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 8月  10 20:09 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

//添加环境变量

[root@mysql ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql ~]# source /etc/profile.d/mysql.sh
[root@mysql ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立数据存放目录

[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/
[root@mysql ~]# ll /opt/
总用量 0
drwxr-xr-x 2 mysql mysql 6 8月  10 20:16 data

//初始化数据库

[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-08-10T12:17:29.959505Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-10T12:17:30.902297Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-10T12:17:31.051861Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-10T12:17:31.392858Z 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: d3ae6f6d-bb68-11e9-82bc-000c2918dd01.
2019-08-10T12:17:31.396859Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-08-10T12:17:31.405610Z 1 [Note] A temporary password is generated for root@localhost: hy)n=ek#&6FU

//配置mysql

[root@mysql ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@mysql ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql ~]# ldconfig -v

//生成配置文件

[root@mysql ~]# 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@mysql ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@mysql ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//启动mysql

[root@mysql ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/mysql.err'.
. SUCCESS! 
[root@mysql ~]# ps -ef|grep mysql
root      18333      1  0 20:22 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     18511  18333  7 20:22 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      18543   1315  0 20:22 pts/0    00:00:00 grep --color=auto mysq
[root@mysql ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128      *:22                   *:*                  
LISTEN      0      100    127.0.0.1:25                   *:*                  
LISTEN      0      128     :::22                  :::*                  
LISTEN      0      100    ::1:25                  :::*                  
LISTEN      0      80      :::3306                :::*                  

//修改密码

//使用临时密码登录

[root@mysql ~]# mysql -uroot -p'hy)n=ek#&6FU'
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 2
Server version: 5.7.22

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> 

//设置新密码

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

mysql> quit
Bye

//验证是否密码修改成功

[root@mysql ~]# mysql -uroot -p'jxy123!'
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.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> 

3. php服务器配置

//关闭防火墙和selinux

[root@php ~]# systemctl stop firewalld
[root@php ~]# systemctl disable firewalld
[root@php ~]# getenforce 
Disabled

//配置yum源

[root@php ~]# cd /etc/yum.repos.d/
[root@php yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@php yum.repos.d]# ls
2019.repo  CentOS7-Base-163.repo  redhat.repo
[root@php ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@php ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@php ~]# yum -y install epel-release
[root@php ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

//安装依赖包

[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  libpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72w-mysqlnd

//下载php

[root@php ~]# cd /usr/src/
[root@php src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
[root@php src]# ls
debug  kernels  php-7.2.8.tar.xz
[root@php src]# tar xf php-7.2.8.tar.xz 
[root@php src]# ls
debug  kernels  php-7.2.8  php-7.2.8.tar.xz
[root@php src]# cd php-7.2.8
[root@php php-7.2.8]# ./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 \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib-dir \
> --with-freetype-dir \
> --with-gettext \
> --enable-json \
> --enable-mbstring \
> --enable-pdo \
> --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --with-readline \
> --enable-shmop \
> --enable-simplexml \
> --enable-sockets \
> --enable-zip \
> --enable-mysqlnd-compression-support \
> --with-pear \
> --enable-pcntl \
> --enable-posix
[root@php php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@php php-7.2.8]# make install

//安装后配置

[root@php php-7.2.8]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@php php-7.2.8]# source /etc/profile.d/php7.sh
[root@php php-7.2.8]# which php
/usr/local/php7/bin/php

//配置php-fpm

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

//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:

include=/usr/local/php7/etc/php-fpm.d/*.conf
//在配置文件最后一行添加如下
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@php ~]# tail /usr/local/php7/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

//修改php-fpm服务器上配置文件,修改监听地址

[root@php ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 192.168.93.131:9000  //ip指向自己
listen.allowed_clients = 192.168.93.129  //添加http服务端的ip地址

//启动php-fpm

[root@php ~]# service php-fpm start
Starting php-fpm  done
[root@php ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128      *:22                   *:*                  
LISTEN      0      100    127.0.0.1:25                   *:*                  
LISTEN      0      128    192.168.93.131:9000                 *:*                  
LISTEN      0      128     :::22                  :::*                  
LISTEN      0      100    ::1:25                  :::*                  
[root@php ~]# ps -ef|grep php
root      39527      1  0 21:09 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    39528  39527  0 21:09 ?        00:00:00 php-fpm: pool www
nobody    39529  39527  0 21:09 ?        00:00:00 php-fpm: pool www
nobody    39530  39527  0 21:09 ?        00:00:00 php-fpm: pool www
nobody    39531  39527  0 21:09 ?        00:00:00 php-fpm: pool www
nobody    39532  39527  0 21:09 ?        00:00:00 php-fpm: pool www
root      39540   1414  0 21:10 pts/0    00:00:00 grep --color=auto php

4. 在nginx服务器上配置

//修改/usr/local/nginx/conf/nginx.conf配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
 server {
        listen       80;
        server_name  192.168.93.131;    //此处填php上的ip地址
 location / {
            root   html;    //存放index.php目录
            index  index.php index.html index.htm;     //此处添加index.php
        }
location ~ \.php$ {          #取消注释,修改配置文件
            root           html;
            fastcgi_pass   192.168.93.131:9000;      //修改为php的ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /var/html/$fastcgi_script_name;  
            include        fastcgi_params;
        }

//创建存放目录中文件为index.php,与nginx配置文件'root'后面一致
[root@nginx ~]# cat > /usr/local/nginx/html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
[root@nginx ~]# cat /usr/local/nginx/html/index.php 
<?php
    phpinfo();
?>

5. 在php服务端上配置

// 修改vim /usr/local/php7/etc/php-fpm.d/www.conf里面的配置文件
[root@php ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
// 搜索listen,修改下面两项
listen = 192.168.93.131:9000     //此处填写php服务端的ip地址
listen.allowed_clients = 192.168.93.129   //修改为nginx服务端的ip地址

//创建/var/html根目录
[root@php ~]# mkdir -p /var/html/
[root@php ~]# cd /var/html/
[root@php html]# vim index.php
[root@php html]# cat index.php 
<?php
    phpinfo();
?>
[root@php html]# ll
总用量 4
-rw-r--r--. 1 root root 24 8月  27 23:14 index.php
[root@php html]# chmod +x index.php 
[root@php html]# ll
总用量 4
-rwxr-xr-x. 1 root root 24 8月  27 23:14 index.php
[root@php ~]# chown -R 777 /var/html/index.php 
[root@php ~]# ll -d /var/html/index.php 
-rwxr-xr-x. 1 777 root 24 8月  27 23:14 /var/html/index.php

//重启php-pfm服务,并查看端口是否启动
[root@php ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@php ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    192.168.93.131:9000                   *:*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      100      ::1:25                    :::*    

6. 验证

修改/etc/hosts文件,添加域名与IP的映射
目录是在C:\Windows\System32\drivers\etc下的hosts

这里添加的域名为
192.168.93.129(nginx)     www.jxy.com
  • 通过IP访问

在这里插入图片描述

  • 通过域名访问

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值