LNMP架构源码包部署
1、什么是LNMP架构
1-1、LNMP
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
2、部署前准备
2-1、安装架构所需依赖包
2-1-1、安装部署LNMP所需依赖包
[root@localhost ~]# yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof
2-2、下载源码包
百度盘下载源码包
链接:https://pan.baidu.com/s/1zL6JfoBamOXSPxLxB62yaQ
提取码:8bi4
这里直接下载到本地,然后上传到目录/usr/local/src/目录下
[root@localhost src]# ls
boost_1_59_0.tar.gz mcrypt-2.6.8.tar.gz nginx-1.14.1.tar.gz
libiconv-1.14.tar.gz mhash-0.9.9.9.tar.gz pcre-8.41.tar.gz
libmcrypt-2.5.8.tar.gz mysql-5.7.19.tar.gz php-7.1.24.tar.gz
[root@master src]# pwd //当前目录
/usr/local/src
2-3、安装epel扩展yum源
nginx提供gzip模块,需要zlib支持
openssl为nginx提供SSL功能
[root@localhost ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2-4、创建nginx用户
查看系统有无nginx用户、没有则创建
[root@localhost ~]# id nginx
id: nginx: no such user
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
2-4、安装编译环境所需依赖包
[root@localhost ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
3、nginx部署
3-1、解压nginx压缩包
解压并进入nginx目录下
[root@localhost src]# tar -xzvf nginx-1.14.1.tar.gz
[root@localhost src]# tar -xzvf pcre-8.41.tar.gz
[root@localhost src]# cd nginx-1.14.1/
3-1、编译前准备
指定nginx安装目录、默认参数指定
[root@localhost nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
对应参数解释
--prefix 指定安装目录
--with-http_dav_module 启用支持,增加PUT\DELETE\MKCOL,编译开启
--with-http_stub_status_module 获取nginx上次启动以来的工作状态
--with-http_addition_module 作用一个输出过滤器,支持不完全缓冲,分部分相应请求
--with-http_sub_module 允许一些其他文本替换nginx中的一些文本
--with-http_flv_module 支持flv视频格式
--with-http_mp4_module 支持mp4视频格式
--with-pcre pcre源码所在目录
--user nginx用户
--group nginx用户组
3-2、编译 make 、make install
编译生成二进制安装程序并安装
[root@localhost nginx-1.14.1]# make //生成二进制安装程序
[root@localhost nginx-1.14.1]# make install //安装生成的二进制程序
如果make出现报错
[root@localhost nginx-1.14.1]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/local/src/nginx-1.14.1'
make[1]: Nothing to be done for `build'.
make[1]: Leaving directory `/usr/local/src/nginx-1.14.1'
make[1]: 离开目录“/usr/local/src/nginx-1.14.1”
若有以上报错,可以看看目录/usr/local/nginx/是否存在,存在则不用理会该报错。
3-3、配置nginx支持php
3-3-1、备份nginx主配置文件
[root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
3-3-2、修改nginx主配置文件
[root@localhostr ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
1
2 user nginx; //去掉第二行注释,将用户改为nginx
3 worker_processes 1;
4
5 #error_log logs/error.log;
# 去掉65行开始的注释
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:8080;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; //注意修改nginx家目录
70 include fastcgi_params;
71 }
3-4、编写nginx开机自启脚本
启动服务(启动脚本启动)
sbin目录下存放着启动nginx脚本
[root@localhost ~]# /usr/local/nginx/sbin/nginx
3-4-1、配置环境变量文件
[root@localhost ~]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
[root@localhost ~]# chmod +x /etc/profile.d/nginx.sh //添加可执行权限
[root@localhost ~]# /etc/profile.d/nginx.sh
3-4-2、编写nginx启动脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Userage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
3-4-5、配置服务启动和自启动
[root@localhost ~]# chmod +x /etc/init.d/nginx //添加可执行权限
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
3-5、nginx服务检验效果
清空iptables,参看nginx进程信息,再浏览器上验证,则表示nginx已经装好了。
[root@localhost ~]# iptable -F //清空防火墙
[root@localhost sbin]# ps -ef | grep nginx //查看nginx进程
root 100056 1 0 21:41 ? 00:00:00 nginx: master process ./nginx
nginx 100057 100056 0 21:41 ? 00:00:00 nginx: worker process
root 100081 19710 0 21:42 pts/0 00:00:00 grep --color=auto nginx
在浏览器输入服务器ip地址访问nginx默认主页
4、mysql部署
4-1、解决mysql依赖
删除系统自带的boost-*
[root@localhost ~]# yum -y remove boost-*
安装mysql依赖包
[root@localhost ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
创建mysql组和mysql用户
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -M -s /sbin/nologin -g mysql mysql
下载mysql、boost源码包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://liquidtelecom.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
[root@localhost src]# ls
boost_1_59_0.tar.gz mcrypt-2.6.8.tar.gz nginx-1.14.1 pcre-8.41.tar.gz
libiconv-1.14.tar.gz mhash-0.9.9.9.tar.gz nginx-1.14.1.tar.gz php-7.1.24.tar.gz
libmcrypt-2.5.8.tar.gz mysql-5.7.19.tar.gz pcre-8.41
4-2、解压mysql、boost到当前目录
[root@localhost src]# tar -xzvf mysql-5.7.19.tar.gz
[root@localhost src]# tar -xzvf boost_1_59_0.tar.gz
[root@localhost src]# ls
boost_1_59_0 libmcrypt-2.5.8.tar.gz mysql-5.7.19 nginx-1.14.1.tar.gz php-7.1.24.tar.gz
boost_1_59_0.tar.gz mcrypt-2.6.8.tar.gz mysql-5.7.19.tar.gz pcre-8.41
libiconv-1.14.tar.gz mhash-0.9.9.9.tar.gz nginx-1.14.1 pcre-8.41.tar.gz
4-3、规划安装目录
数据目录最好是一个单独的分区或者RAID或者LVM,便于日后的磁盘维护和扩容
安装目录: /var/lib/mysql
数据目录: /var/lib/mysql/data
[root@localhost src]# mkdir -p /var/lib/mysql/data //创建存储目录
[root@localhost src]# chown -R mysql:mysql /var/lib/mysql/ 更改所有人所有组
[root@localhost src]# ll -d /var/lib/mysql/
drwxr-xr-x. 3 mysql mysql 18 6月 30 15:31 /var/lib/mysql/
4-4、编译前准备
指定安装参数
[root@localhost src]# cd mysql-5.7.19/
[root@localhost mysql-5.7.19]# cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql -DMYSQL_DATADIR=/var/lib/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost_1_59_0
各参数对应注释
#-DCMAKE_INSTALL_PREFIX=/var/lib/mysql 指定MySQL程序的安装目录,默认/usr/local/mysql
# -DMYSQL_DATADIR=/var/lib/mysql/data 数据文件目录
# -DSYSCONFDIR=/etc 初始化参数文件目录
# -DWITH_MYISAM_STORAGE_ENGINE=1 指定静态编译到 mysql 的存储引擎,MyISAM,MERGE
# -DWITH_INNOBASE_STORAGE_ENGINE=1
# -DWITH_MEMORY_STORAGE_ENGINE=1
# -DWITH_READLINE=1
# -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock socket文件路径,默认/tmp/mysql.sock
# -DMYSQL_TCP_PORT=3306 服务端口号,默认 3306
# -DENABLED_LOCAL_INFILE=1 指定是否允许本地执行
# -DWITH_PARTITION_STORAGE_ENGINE=1
# -DEXTRA_CHARSETS=all
# -DDEFAULT_CHARSET=utf8 指定服务器默认字符集,默认 latin1
# -DDEFAULT_COLLATION=utf8_general_ci 指定服务器默认的校对规则,默认 latin1_general_ci
# -DDOWNLOAD_BOOST=1
#-DWITH_BOOST=/usr/local/src/boost_1_59_0
4-5、编译 make 、make install
编译生成二进制安装程序并安装
[root@localhost mysql-5.7.19]# make //生成二进制安装程序
[root@localhost mysql-5.7.19]# make install //安装生成的二进制程序
4-6、更改mysql配置文件
[root@master ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
basedir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
datadir=/var/lib/mysql/data //添加以下7行指定各个设置配置
port=3306
character-set-server=utf8
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
[mysql]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
4-7、mysql初始化工作
4-7-1、添加环境变量、使其生效
[root@localhost ~]# vim /etc/profile.d/mysql.sh
export PATH=/var/lib/mysql/bin:$PATH
[root@localhost ~]# chmod +x /etc/profile.d/mysql.sh
[root@localhost ~]# /etc/profile.d/mysql.sh
4-7-2、生成服务启动脚本、初始化数据库
[root@localhost ~]# cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
[root@localhost ~]# cp /var/lib/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
4-7-3、登录mysql并设置root用户密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.19 Source distribution
Copyright (c) 2000, 2017, 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 for root@localhost = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
5、php部署
5-1、解决php依赖
官网地址:http://php.net/
下载php依赖包
[root@localhost src]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
[root@localhost src]# wget http://iweb.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@localhost src]# wget http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
安装php依赖包
[root@localhost ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
解压php源码包
[root@localhost ~]# tar xzvf php-7.1.24.tar.gz
5-2、编译前准备
指定安装目录参数
[root@localhost src]# cd php-7.1.24/
[root@localhost php-7.1.24]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/ --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
5-3、编译并安装
生成二进制安装程序并安装
[root@localhost php-7.1.24]# make
[root@localhost php-7.1.24]# make install
5-4、生成php自身配置文件
5-4-1、拷贝修改PHP、PHP-fpm配置文件
[root@localhost ~]# cp /usr/local/src/php-7.1.24/php.ini-production /usr/local/php/php.ini
[root@localhost ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
23 user = nginx //注意23、24行参数
24 group = nginx
5-4-2、生成 php-fpm 启动脚本
[root@localhost ~]# cp /usr/local/src/php-7.1.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
5-4-3、设置php-fpm启动和自启动
[root@localhost ~]# chmod +x /etc/init.d/php-fpm
[root@localhost ~]# chkconfig php-fpm on
[root@localhost ~]# /etc/init.d/php-fpm start
Starting php-fpm done
检验php-fpm是否启动
[root@localhost ~]# netstat -antup | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 77628/php-fpm: mast
5-4-4、测试LNMP的PHP支持
[root@localhost ~]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php
[root@localhost ~]# iptables -F
5-5、重启服务器访问以下地址:
http://192.168.128.109/index.php