分离部署lnmp
环境说明:
服务类型 | ip地址 | 应用 | 操作系统 |
---|---|---|---|
nginx | 192.168.47.134 | nginx | centos7/redhat7 |
mysql | 192.168.47.135 | mysql | centos7/redhat7 |
php | 192.168.47.136 | php | centos7/redhat7 |
1.安装nginx
//关闭防火墙和selinux
[root@nginx ~]# systemctl stop firewalld
Disabled
//创建系统用户和组
[root@nginx ~]# groupadd -r nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin -g nginx nginx
//安装依赖环境
[root@nginx ~]# yum -y groups mark install 'Development Tools'
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Marked install: Development Tools
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
[root@nginx ~]# ll -d /var/log/nginx/
drwxr-xr-x. 2 nginx nginx 6 8月 28 01:44 /var/log/nginx/
//下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
--2019-08-28 01:45:18-- http://nginx.org/download/nginx-1.16.1.tar.gz
正在连接 nginx.org (nginx.org)|62.210.92.35|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
100%[================================================>] 1,032,630 25.3KB/s 用时 41s
2019-08-28 01:46:01 (24.5 KB/s) - 已保存 “nginx-1.16.1.tar.gz” [1032630/1032630])
//编译安装
[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 kernels nginx-1.16.1 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
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# . /etc/profile.d/nginx.sh
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
2.安装mysql
//关闭防火墙和selinux
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
[root@mysql ~]# vim /etc/selinux/config
Disabled
//安装依赖包
[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 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 ~]# cd /usr/local/
[root@mysql local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
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
lrwxrwxrwx. 1 root root 36 8月 28 02:16 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//添加环境变量
[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH'> /etc/profile.d/mysql.sh
[root@mysql ~]# . /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 -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 8月 28 02:19 /opt/data/
//初始化数据库
2019-08-27T18:34:47.785057Z 0 [Warning] InnoDB: New log files created, LSN=45790
[root@mysql ~]# echo 'Ssaidegjswle' > pass
[root@mysql ~]# cat pass
S<rife0jsw%e
//请注意,这个命令的最后会生成一个临时密码,此处密码是S<rife0jsw%e
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
//配置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
ldconfig: 无法对 /libx32 进行 stat 操作: 没有那个文件或目录
ldconfig: 多次给出路径“/usr/lib”
ldconfig: 多次给出路径“/usr/lib64”
......
//生成配置文件
[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 4113 3509 0 02:42 pts/1 00:00:00 grep --color=auto mysql
[root@mysql ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
LISTEN 0 80 :::3306 :::*
//修改密码
//使用临时密码登陆
[root@mysql ~]# mysql -uroot -p
Enter password:
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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//新密码登陆
[root@mysql ~]# mysql -uroot -p123456
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
mysql>
3.安装php
//关闭防火墙和selinux
[root@php~]# systemctl stop firewalld
[root@php ~]# systemctl disable firewalld
[root@php ~]# cd /etc/yum.repos.d/
[root@php yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2019-08-28 02:46:15-- http://mirrors.163.com/.help/CentOS7-Base-163.repo
正在解析主机 mirrors.163.com (mirrors.163.com)... 59.111.0.251
正在连接 mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1572 (1.5K) [application/octet-stream]
正在保存至: “CentOS7-Base-163.repo”
100%[================================================>] 1,572 --.-K/s 用时 0s
2019-08-28 02:46:16 (19.4 MB/s) - 已保存 “CentOS7-Base-163.repo” [1572/1572])
[root@php yum.repos.d]# yum -y install epel-release
[root@php yum.repos.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
警告:/var/tmp/rpm-tmp.zQHr6u: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:webtatic-release-7-3 ################################# [100%]
//安装依赖包
//安装编译工具
[root@php~]#yum -y install gcc gcc-c++
//下载php
[root@php ~]# cd /usr/src/
[root@php src]# ls
debug kernels php-7.2.8.tar.xz
[root@php src]# tar xf php-7.2.8.tar.xz
debug kernels php-7.2.8 php-7.2.8.tar.xz
[root@php src]# cd php-7.2.8/
//编译安装php
[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-posix
[root@php php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@php php-7.2.8]#make && make install
//安装后配置
[root@ytservice php-7.2.8]# cd
[root@ytservice ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@ytservice ~]# source /etc/profile.d/php7.sh
[root@ytservice ~]# which php
/usr/local/php7/bin/php
[root@ytservice ~]# php -v
PHP 7.2.8 (cli) (built: Aug 28 2019 04:40:39) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
//配置php-fpm
[root@ytservice ~]# cd /usr/src/
[root@ytservice src]# ls
debug kernels php-7.2.8 php-7.2.8.tar.xz
[root@ytservice src]# cd php-7.2.8/
[root@ytservice php-7.2.8]# cp php.ini-production /etc/php.ini
cp:是否覆盖"/etc/php.ini"? y
[root@ytservice php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@ytservice php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
//编辑php-fpm的配置文件(/us/local/etc/php-fpm.conf)
//配置fpm的相关选项为你所需要的值:
[root@ytservice php-7.2.8]# cd
[root@ytservice ~]# vim /usr/local/php7/etc/php-fpm.conf (添加以下四行)
pm.max_children = 50 # 最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 # 启动时启动5个进程
pm.min_spare_servers = 2 # 最小空闲进程数
pm.max_spare_servers = 8 # 最大空闲进程数
[root@ytservice ~]# tail /usr/local/php7/etc/php-fpm.conf
; 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 # 最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 # 启动时启动5个进程
pm.min_spare_servers = 2 # 最小空闲进程数
pm.max_spare_servers = 8 # 最大空闲进程数
//启动php-fpm
[root@ytservice ~]# service php-fpm start
Starting php-fpm done
[root@ytservice ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
nobody 58289 58288 0 04:44 ? 00:00:00 php-fpm: pool www
nobody 58290 58288 0 04:44 ? 00:00:00 php-fpm: pool www
nobody 58291 58288 0 04:44 ? 00:00:00 php-fpm: pool www
nobody 58292 58288 0 04:44 ? 00:00:00 php-fpm: pool www
nobody 58293 58288 0 04:44 ? 00:00:00 php-fpm: pool www
root 58302 37765 0 04:45 pts/2 00:00:00 grep --color=auto php
4.安装后配置
nginx服务端
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
location / {
root html;
index index.php index.html index.htm; ##注:添加index.php
}
}
location ~ \.php$ {
fastcgi_pass 192.168.47.1369000; ##注:修改为php服务器地址
fastcgi_index index.php;
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
//启动服务并查看端口
[root@nginx ~]# pkill nginx
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
//在存放网页的目录中创建index.php文件(与配置文件中的"root"项对应)
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# vim index.php
[root@nginx html]# cat index.php
<?php
phpinfo();
?>
//配置/usr/local/php7/etc/php-fpm.d/www.conf配置文件,修改内容如下:
[root@ytservice ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 192.168.47.136:9000 #监听ip地址为php服务器的ip地址,端口号为9000
;listen.allowed_clients = 192.168.47.134 #允许nginx服务器(ip:192.168.47.134)访问
//在php服务器上创建/var/www/根目录
[root@ytservice ~]#mkdir /var/www
//在创建目录中创建index.php文件,文件内容如下:
[root@ytservice ~]# cat > /var/www/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
//重启php-fpm服务,并查看端口
[root@ytservice ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@ytservice ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 192.168.47.136:9000 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
```