从yum源中获得官方为我们提供的mysql、php等其功能有限,而我们自行编译,是为了定制化,可以在编译过程中添加我们需要的功能模块,需要什么服务就安装什么,这样系统就比较纯净。
实验环境
主机(版本:ip) | 功用 |
---|---|
虚拟机server1(rhel6.5:172.25.2.1) | 搭建lnmp |
真机(rhel7.3:172.25.2.250) | 测试机 |
首先,封装一个rhel6.5的母盘,然后快照一个镜像
一、源码编译MySQL
1、解压安装mysql:tar zxf mysql-boost-5.7.17.tar.gz
2、安装cmake,cmake相当于configure,用来编译yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm
3、进入解压目录,编译:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \ #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \ #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎
-DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \ #校验字符
-DEXTRA_CHARSETS=all \ #安装所有扩展字符集
-DWITH_BOOST=boost/boost_1_59_0/
- 当报错:
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
This CMake script will look for boost in <directory>. If it is not there,
it will download and unpack it (in that directory) for you.
If you are inside a firewall, you may need to use an http proxy:
export http_proxy=http://example.com:80
解决:缺少 -DWITH_BOOST= 参数 编译时添加-DWITH_BOOST=boost/boost_1_59_0/
4、发现报错,根据提示解决依赖性问题,需要安装:yum install -y gcc gcc-c++ ncurses-devel
5、再次重新编译:
- 重新编译时,需要清除原来的对象文件和缓存信息:
make clean;rm -fr CmakeCache.txt
6、发现有一个警告:
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
sql/CMakeLists.txt:549 (INCLUDE)
解决:yum install -y bison
,再次重新编译。
7、make && make install
过程时间较长,如果make还有问题,删除原来的目录,重新解压编译
8、复制启动脚本/root/mysql-5.7.17/support-files/mysql.server
到/etc/init.d/mysqld
,并给可执行权限chmod +x /etc/init.d/mysqld
9、将原来的/etc/my.cnf
备份,重新拷贝安装包里的到/etc
下
[root@server1 support-files]# cp my.cnf my.cnf.bak
[root@server1 support-files]# cp my-default.cnf /etc/my.cnf
10、编辑/etc/my.cnf
修改配置:
basedir = /usr/local/lnmp/mysql
datadir = /usr/local/lnmp/mysql/data
socket = /usr/local/lnmp/mysql/data/mysql.sock
11、添加用户和组,指定id为27,因为rpm默认安装时mysql用户的id为27,并非强制
[root@server1 support-files]# groupadd -g 27 mysql
[root@server1 support-files]# useradd -u 27 -g 27 mysql
[root@server1 support-files]# chown mysql.mysql /usr/local/lnmp/mysql -R
# 修改/usr/local/lnmp/mysql目录的权限,使其对mysql用户可写
12、将mysql命令
添加到环境变量
[root@server1 support-files]# vim ~/.bash_profile
添加:PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
[root@server1 support-files]# source ~/.bash_profile
13、初始化mysql,会生成一个临时密码,用于登录mysql(要记住此密码)
14、为了安全,再将mysql目录的所有者改为root,但是 数据目录所有者必须是mysql,不然mysql用户不能写
[root@server1 support-files]# chown root /usr/local/lnmp/mysql -R
[root@server1 support-files]# chown mysql /usr/local/lnmp/mysql/data -R
15、启动mysql:/etc/init.d/mysqld start
,使用初始化密码登录mysql
16、执行show databases;
会出现报错,这是因为没有进行安全初始化
17、执行:mysql_secure_installation
,然后会提示是否启用密码检测插件,直接回车不启用,否则会要求密码有大小写和特殊字符等要求,剩余全部选 y
此时,再次登录数据库,可以查看数据库了。
可以看到数据目录下生成了套节字文件
最后,可以看到mysql服务已开启。
二、源码编译安装PHP
1、解压php的tar包tar jxf php-5.6.35.tar.bz2
2、进入解压后的目录/root/php-5.6.35
,开始编译
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \ #指定php.ini位置
--with-openssl --with-snmp --with-gd --with-zlib --with-curl \
--with-libxml-dir --with-png-dir \
--with-jpeg-dir --with-freetype-dir --with-gmp \ # --with-freetype-dir 打开对freetype字体库的支持
--with-gettext --with-pear --enable-mysqlnd \
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
# --with-MySQL=mysql安装目录,对mysql的支持 ;--with-MySQLi=mysqli文件目录,优化支持
--enable-inline-optimization --enable-soap --enable-ftp \
--enable-sockets --enable-mbstring --enable-fpm \ #--enable-fpm参数即可开启PHP-FPM
--with-fpm-user=nginx --with-fpm-group=nginx \
# 编译的时候指定了php的用户是nginx用户,故要建立nginx用户
--with-mcrypt --with-mhash
3、出现报错:
- 报错1:
解决:yum install -y libxml2-devel
,再次编译
- 报错2:
解决:yum install -y openssl-devel
,再次编译
- 报错3:
解决:yum install -y libcurl-devel
,再次编译
- 报错4:
解决:yum install -y libjpeg-turbo-devel-1.2.1-1.el6.x86_64
,再次编译
- 报错5:
解决:yum install -y libpng-devel-1.2.49-1.el6_2.x86_64
,再次编译
- 报错6,及解决:
- 报错7:
解决:
- 报错8,及解决:
4、再次编译,成功。
5、安装make && make install
6、编辑配置文件php-fpm.conf
:
[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/etc
[root@server1 etc]# ls
pear.conf php-fpm.conf.default
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
# 复制一份配置文件模版
[root@server1 etc]# vim php-fpm.conf # 编辑fastcgi的进程管理文件
25 pid = run/php-fpm.pid
149 user = nginx
150 group = nginx
7、拷贝更改php主配置文件/usr/local/lnmp/php/etc/php.ini
,修改时区
[root@server1 ~]# cd /root/php-5.6.35
[root@server1 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server1 php-5.6.35]# vim /usr/local/lnmp/php/etc/php.ini
date.timezone = Asia/Shanghai
8、拷贝php启动脚本至/etc/init.d/
[root@server1 php-5.6.35]# cd /root/php-5.6.35/sapi/fpm
[root@server1 fpm]# ls
config.m4 init.d.php-fpm.in php-fpm.8 php-fpm.service tests
CREDITS LICENSE php-fpm.8.in php-fpm.service.in www.conf.in
fpm Makefile.frag php-fpm.conf status.html
init.d.php-fpm php-fpm php-fpm.conf.in status.html.in
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm
[root@server1 fpm]# useradd nginx
[root@server1 fpm]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 fpm]# /etc/init.d/php-fpm start
Starting php-fpm done
9、此时,netstat -tlnp
可以看到php已经启动,默认php服务开启9000端口。
三、源码编译nginx
1、解压nginx的tar包:tar zxf nginx-1.17.4.tar.gz
2、进入nginx解压后的目录,修改src/core/nginx.h
文件,删除nginx版本号
3、编辑auto/cc/gcc
文件,关闭debug模式
4、开始编译安装
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx
5、出现报错,安装yum install pcre-devel -y
,然后再次编译。
6、编译成功,安装make && make install
7、编辑nginx配置文件/usr/local/lnmp/nginx/conf/nginx.conf
,开启php模块
[root@server1 nginx-1.17.4]# vim /usr/local/lnmp/nginx/conf/nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm; (添加php页面)
46 }
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
8、创建软链接,将nginx启动脚本链接到/usr/local/sbin/
,然后启动nginx。
[root@server1 nginx-1.17.4]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
[root@server1 nginx-1.17.4]# nginx -t
[root@server1 nginx-1.17.4]# nginx
9、此时,可以看到php、nginx、mysql服务全部开启
测试:
1、访问:http://172.25.2.1
,可以看到nginx默认发布页面
2、在nginx默认发布目录添加 index.php
vim /usr/local/lnmp/nginx/html/index.php
<?php
phpinfo()
?>
访问:http://172.25.2.1/index.php
,可以看到php默认发布页面
至此,lnmp环境就已经搭建完成了。