源码编译LNMP环境

 

一、环境准备:

1.部署LNMP架构需要安装依赖包

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

 

下载PCRE库https://ftp.pcre.org/pub/pcre/

 [root@Wg64 ~]#unzip pcre-8.38.zip -d /usr/local/src

注:解压即可,不用安装,Nginx安装时指定pcre的解压路径即可

[root@Wg64 ~]# tar zxf pcre-8.38.tar.gz -C /usr/local/src/

 

2、上传源码包

图片.png

 

二、安装LNMP环境

确认已安装所需依赖包:

[root@Wg64 ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*

 

1、源码编译安装Nginx

(1)、解压安装文件

[root@Wg64 ~]# tar zxf nginx-1.10.3.tar.gz -C /usr/local/src/

[root@Wg64 ~]# cd /usr/local/src/nginx-1.10.3/

 

(2)、创建Nginx运行用户

[root@Wg64 nginx-1.10.3]# useradd -M -s /sbin/nologin nginx

 

(3)、参数配置

[root@Wg64 nginx-1.10.3]#  ./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.38 --user=nginx --group=nginx

注:

--with-http_dav_module                   #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)

                                    默认关闭,需要编译开启

--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=/usr/local/src/pcre-8.37   #需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助

(4)编译安装

[root@Wg64 nginx-1.10.3]# make -j 2 && make install

 

查看目录文件

[root@Wg64 nginx-1.10.3]# ll /usr/local/nginx/

total 16

drwxr-xr-x 2 root root 4096 Jan 11 20:22 conf  #Nginx相关配置文件

drwxr-xr-x 2 root root 4096 Jan 11 20:22 html  #网站根目录

drwxr-xr-x 2 root root 4096 Jan 11 20:22 logs  #日志文件

drwxr-xr-x 2 root root 4096 Jan 11 20:22 sbin   #Nginx启动脚本 

 

(5)配置Nginx支持php文件

[root@Wg64 nginx-1.10.3]# vim /usr/local/nginx/conf/nginx.conf

 

修改用户为nginx:

2 #user  nobody;

  3 user    nginx nginx;

image.png

启用PHP支持

第66行始 修改为:

66         location ~ \.php$ {

 67             root           html;

 68             fastcgi_pass   127.0.0.1:9000;

 69             fastcgi_index  index.php;

 70       fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;

 71             include        fastcgi_params;

 72         }

image.png

 

(6)启动Nginx服务

[root@Wg64 nginx-1.10.3]# /usr/local/nginx/sbin/nginx

[root@Wg64 nginx-1.10.3]# netstat -anptu | grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      51896/nginx      

 

(7)优化Nginx启动命令执行路径

[root@Wg64 nginx-1.10.3]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

 

(8)生成服务启动脚本

[root@Wg64 nginx-1.10.3]# 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

image.png

 

(9)配置服务开机自动启动

[root@Wg64 nginx-1.10.3]# chmod +x /etc/init.d/nginx

 [root@Wg64 nginx-1.10.3]# chkconfig --add nginx

[root@Wg64 nginx-1.10.3]# chkconfig nginx on

 

(10)先关闭iptables 再浏览器访问验证:

image.png

扩展:Nginx维护命令

#检查配置文件是否有语法错误

 [root@Wg64 nginx-1.10.3]# 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

 

      

#查看Nginx版本和配置参数

[root@Wg64 nginx-1.10.3]# nginx -V

nginx version: nginx/1.10.3

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

configure arguments: --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.38 --user=nginx --group=nginx

注:重新编译时,一定要查看以前的编译配置,只需在原有配置参数后添加新的参数即可

 

#不用停止服务,重载Nginx配置文件

[root@Wg64 nginx-1.10.3]# nginx -s reload          

 

 

2、源码编译安装mysql(5.7版本)

下载地址:

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz

#(MySQL从5.7版本之后,boost是必须的,建议把系统自带的boost库卸载,源码编译安装高版本

Boost库是为C++语言标准库提供扩展的一些C++程序库的总称

(1)环境准备:

[root@Wg64 ~]# ls

mysql-boost-5.7.18.tar.gz  nginx-1.10.3.tar.gz  nginx.conf  pcre-8.38.tar.gz  php-7.0.18.tar.gz

 

#卸载系统自带boots库

[root@Wg64 ~]# yum -y remove boost-*  

#卸载系统自带的mysql

[root@Wg64 ~]#  yum -y remove mysql

[root@Wg64 ~]# rpm -qa | grep mysql

mysql-libs-5.1.73-5.el6_6.x86_64

[root@Wg64 ~]#  rpm -e --nodeps  mysql-libs-5.1.73-5.el6_6.x86_64

[root@Wg64 ~]# rpm -qa | grep mysql

#安装必要的资源包

建议使用网络yum源,RHEL6.5光盘中自带的软件包版本不够,mysql-boost-5.7.18.tar.gz的编译对软件包的版本要求比较高,其中cmake的版本要不低于2.8

#安装依赖包

[root@Wg64 ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

 

(2)添加用户和组

[root@Wg64 ~]# groupadd mysql

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

 

(3)创建安装目录和数据存放目录

[root@Wg64 ~]# mkdir -p /server/mysql

 

(4)上传源码包 并解压源码包

[root@HK64 LAMP]# ls

image.png

 

[root@Wg64 ~]# tar zxf mysql-boost-5.7.18.tar.gz -C /server/

 

(5)将boost移到/server/

image.png 

[root@HK64 server]# cd mysql-5.7.18/   (包含boost库的文件)

image.png 

#将boots文件移动到/server/mysql/下

[root@Wg64 mysql-5.7.18]# mv boost/ /server/mysql

[root@Wg64 mysql-5.7.18]# cd /server/mysql

[root@Wg64 mysql]# ls

boost

 

(6)配置参数

[root@Wg64 mysql]# cd ../

[root@Wg64 server]# ls

mysql  mysql-5.7.18

[root@Wg64 server]# cd mysql-5.7.18/

[root@Wg64 mysql-5.7.18]#cmake -DCMAKE_INSTALL_PREFIX=/server/mysql  -DMYSQL_DATADIR=/server/mysql/data -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/server/mysql/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=l -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=/server/mysql/boost

image.png

这些编译参数的帮助寻找方法:

http://www.mysql.com→→Documentation→→选择对应的版本(5.7)Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options

最终的URL https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html               

 

DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否允许本地执行LOAD DATA INFILE,默认OFF
WITH_COMMENT:指定编译备注信息
WITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
SYSCONFDIR:初始化参数文件目录
MYSQL_DATADIR:数据文件目录
MYSQL_TCP_PORT:服务端口号,默认3306
MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock

 

编译

mysql-5.7.18.tar.gz编译时会占用大量的系统资源,建议使用多个核心同时进行编译,否则可能会编译失败

 

(7)编译

#查看服务器cpu数

[root@Wg64 mysql-5.7.18]# grep processor /proc/cpuinfo | wc -l

2

[root@Wg64 mysql-5.7.18]# make -j 2

8)安装

[root@xuegod63 mysql-5.7.18]# make install


(9)修改目录权限

[root@Wg64 mysql-5.7.18]# chown -R mysql:mysql /server/mysql/

 

(10)生成配置文件

#将原服务器配置文件移动备份或删除

[root@Wg64 mysql-5.7.18]# mv /etc/my.cnf{,.bak}

 

#因5.7版本无此配置模板文件/server/mysql/support-files/my-default.cnf   

#自行编写my.cnf文件

[root@Wg64 mysql-5.7.18]#vim /etc/my.cnf

[mysqld]

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#default

user = mysql

basedir = /server/mysql

datadir = /server/mysql/data

port = 3306

pid-file = /server/mysql/data/mysql.pid

socket = /server/mysql/mysql.sock

character-set-server=utf8

[client]

socket = /server/mysql/mysql.sock

 

(11)生成服务启动脚本

grep chkconfig ./* -R  -color

image.png

[root@Wg64 mysql]# cp /server/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@Wg64 mysql]# chmod +x /etc/init.d/mysqld

[root@Wg64 mysql]# chkconfig --add mysqld

[root@Wg64 mysql]# chkconfig mysqld on

[root@Wg64 mysql]# chkconfig --list mysqld

mysqld            0:off       1:off       2:on 3:on 4:on 5:on 6:off

 

(12)初始化数据库

[root@Wg64 mysql]# /server/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/server/mysql --datadir=/server/mysql/data  

image.png

 

(13)启动服务

[root@Wg64 mysql]# service mysqld restart

 ERROR! MySQL server PID file could not be found!

Starting MySQL.Logging to '/server/mysql/data/Wg64.err'.

 SUCCESS!

[root@Wg64 mysql]# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL. SUCCESS!

 

(15)优化调用命令路径

#添加path路径: vim /etc/profile 添加下面2行 在文件的结尾

export MYSQL_HOME=/server/mysql

export PATH=$PATH:$MYSQL_HOME/bin

image.png

#使修改生效

source /etc/profile


[root@Wg64 mysql]# ln -s /server/mysql/bin/* /usr/local/bin/

 

(14)修改mysql密码测试登入:

 

[root@Wg64 ~]# mysqladmin -u root password "123456"

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

测试登入成功:

image.png

 

 

 

 

 

3、源码编译安装PHP

环境准备:

#安装epel扩展yum源

[root@Wg64 php-7.0.18]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

 

#yum安装解决

yum -y install php-mcrypt  libmcrypt  libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel

 

 

 

(1)、解压PHP包并配置:

[root@Wg64 ~]# tar zxf php-7.0.18.tar.gz -C /usr/local/src/

[root@Wg64 ~]# cd /usr/local/src/php-7.0.18/

 

(2)、配置参数

[root@Wg64 php-7.0.18]# ./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

image.png

参数选项可参考http://php.net/manual/zh/configure.about.php官方中文手册

 

--with-config-file-path                   #设置 php.ini 的搜索路径。默认为 PREFIX/lib

--with-mysql                          #mysql安装目录,对mysql的支持 7.0版本没有此参数

--with-mysqli                          #mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。是一个数据库驱动

--with-iconv-dir                        #种字符集间的转换

--with-freetype-dir                     #打开对freetype字体库的支持

--with-jpeg-dir                         #打开对jpeg图片的支持

--with-png-dir                           #打开对png图片的支持

--with-zlib                            #打开zlib库的支持,实现GZIP压缩输出     

--with-libxml-dir=/usr                     #打开libxml2库的支持,libxml是一个用来解析XML文档的函数库

--enable-xml                          #支持xml文档

--disable-rpath                           #关闭额外的运行库文件

--enable-bcmath                        #打开图片大小调整,用到zabbix监控的时候用到了这个模块

--enable-shmop                         #shmop共享内存操作函数,可以与c/c++通讯

--enable-sysvsem                            #加上上面shmop,这样就使得你的PHP系统可以处理相关的IPC函数(活动在内核级别)。

--enable-inline-optimization          #优化线程

--with-curl                             #打开curl浏览工具的支持 

--with-curlwrappers                     #运用curl工具打开url流 ,新版PHP5.6已弃用

--enable-mbregex                      #支持多字节正则表达式

--enable-fpm                          #CGI方式安装的启动程序,PHP-FPM服务

--enable-mbstring                       #多字节,字符串的支持

--with-gd                              #打开gd库的支持,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。

--enable-gd-native-ttf                     #支持TrueType字符串函数库

--with-openssl                           #打开ssl支持

--with-mhash                         #支持mhash算法扩展

--enable-pcntl                           #freeTDS需要用到的,pcntl扩展可以支持php的多线程操作

--enable-sockets                         #打开 sockets 支持

--with-xmlrpc                          #打开xml-rpc的c语言

--enable-zip                           #打开对zip的支持

--enable-soap                           #扩展库通过soap协议实现了客服端与服务器端的数据交互操作

--with-mcrypt                         #mcrypt算法扩展

 

(3)编译并安装

[root@Wg64 php-7.0.18]# make -j 2

image.png

[root@Wg64 php-7.0.18]# make install

image.png

 

(4)配置文件设置

#1、修改fpm配置php-fpm.conf.default文件名称

[root@Wg64 php-7.0.18]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf

#2、修改运行用户和组

[root@xuegod63 php-7.0.5]#vim /usr/local/php/etc/php-fpm.conf

user = nginx

group = nginx

image.png

#3、复制php.ini配置文件

[root@Wg64 php-7.0.18]# cp /usr/local/src/php-7.0.18/php.ini-production /usr/local/php/php.ini

 

(5)设置启动脚本

#1、复制php-fpm启动脚本到init.d

[root@Wg64 php-7.0.18]#cp /usr/local/src/php-7.0.18/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

#2、赋予执行权限

[root@Wg64 php-7.0.18]# chmod +x /etc/init.d/php-fpm

#3、添加为启动项

[root@Wg64 php-7.0.18]# chkconfig --add php-fpm

[root@Wg64 php-7.0.18]# chkconfig php-fpm --list

php-fpm          0:off       1:off       2:on 3:on 4:on 5:on 6:off

#4、设置开机启动

[root@Wg64 php-7.0.18]# chkconfig php-fpm on

#5、启动服务

[root@Wg64 php-7.0.18]# service php-fpm start

Starting php-fpm  done

#6、查看端口监听状态

[root@Wg64 php-7.0.18]#netstat -antpu | grep php-fpm

image.png

 

(6)验证:

[root@Wg64 php-7.0.18]# vim /usr/local/nginx/html/test.php

<?php

        phpinfo();

?>

 

image.png

 

 

 


猜你喜欢

转载自blog.51cto.com/13719714/2110940