温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

源码编译安装LNMP

发布时间:2020-07-12 12:35:59 来源:网络 阅读:632 作者:coolner 栏目:建站服务器

一、nginx概述

       LNMP=Linux Nginx Mysql PHP
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器。Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。


       Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度BWS、新浪、网易、腾讯等

Nginx官方网站

       RamBler:http://www.rambler.ru/                Nginx:http://nginx.org/
源码编译安装LNMP


在Nginx中,我们使用的是php-fpm来对php页面解析,PHP-FPM其实是PHP源代码的一个补丁,指在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,再编译安装PHP后才可以使用
       从PHP5.3.3开始,PHP中直接整合了PHP-FPM,所以从PHP5.3.3版本以后,不需要下载PHP-FPM补丁包了,下面是PHP-FPM官方发出来的通知:
       http://php-fpm.org/download

二、Nginx的工作原理
Nginx 本身只支持静态页面的处理,当客户端访问php页面的时候,nginx会将php转到php-fpm也处理,
php-fpm服务会把php页面解析成html文件给nginx处理,nginx返回给客户端处理
这里需要结合Apache的工作,对PHP文件处理过程的区别
       1:Nginx是通过php-fpm这个服务来处理php文件
       2:Apache是通过libphp5.so这个模块来处理php文件
Apache:

源码编译安装LNMP


Nginx:
源码编译安装LNMP


       Apache的libphp5.so随着apache服务器一起运行,而Nginx和php-fpm是各自独立运行,所以在服务的运行过程中,Nginx和php-fpm都需要分别启动!
修改Nginx配置文件,启动nginx服务,修改php配置文件,启动php-fpm服务。


三、安装环境

系统环境:CentOS release 6.9 64bit  
软件名称:libmcrypt-2.5.8、mysql-5.6.26、nginx-1.9.4、pcre-8.37、php-5.6.13

软件安装位置:/usr/local/

数据存放位置:/usr/local/nginx/html、/usr/local/mysql/data

日志存放位置:/usr/local/nginx/logs、/usr/local/mysql/logs


四、安装lnmp

1、编译安装nginx

1)安装开发工具包

yum -y groupinstall "Development Tools" "Development Libraries"


2)安装依赖包

yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* pcre-devel


3)在home创建lnmp文件夹,将对应软件包上传到这个文件夹

[root@centos-6 lnmp]# ll
total 50292
-rw-r--r-- 1 root root   875425 Jun 10 07:10 libmcrypt-2.5.8.tar.bz2
-rw-r--r-- 1 root root 33278710 Jun 10 07:10 mysql-5.6.26.tar.gz
-rw-r--r-- 1 root root   832104 Jun 10 07:10 nginx-1.8.0.tar.gz
-rw-r--r-- 1 root root   866423 Jun 10 07:11 nginx-1.9.4.tar.gz
-rw-r--r-- 1 root root  1557603 Jun 10 07:10 pcre-8.37.tar.bz2
-rw-r--r-- 1 root root 14077633 Jun 10 07:10 php-5.6.13.tar.bz2
[root@centos-6 lnmp]#


4)解压pcre软件包

解压此安装包即可,不需要安装,Nginx需要指定pcre的源码不是安装后的路径,此包的功能是支持地址重写rewrite功能  pcre的依赖可以yum安装pcre和pcre-devel解决!

tar jxvf pcre-8.37.tar.bz2 -C /usr/local/src/


5)编译安装nginx

[root@centos-6 lnmp]# tar zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@centos-6 lnmp]# cd /usr/local/src/nginx-1.9.4/[root@centos-6 nginx-1.9.4]# ./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.37


6)功能模块说明:

--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查看帮助

7)采用多线程编译加快速度

[root@centos-6 nginx-1.9.4]# make -j 4 && make install


8)创建没家目录及登录权限的nginx运行帐号

[root@centos-6 lnmp]# useradd -M -s /sbin/nologin nginx

9)配置nginx支持php及修改运行用户

[root@centos-6 nginx-1.9.4]#vim /usr/local/nginx/conf/nginx.conf

#user  nobody;                 
user nginx nginx;                   #添加此行


#location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}                      #找到上面这段内容,将这段内容复制,去掉#且修改为如下


location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;      #将scripts修改为nginx的html,即Nginx页面目录,因为要处理的php文件也在这个目录下
    include        fastcgi_params;
}

10)启动nginx并查看状态是否启动成功

[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx
[root@centos-6 nginx-1.9.4]# ps -aux | grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      7715  0.0  0.0  21756   840 ?        Ss   07:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     7716  0.0  0.0  22108  1412 ?        S    07:38   0:00 nginx: worker process      
root      7718  0.0  0.0 103328   876 pts/2    S+   07:38   0:00 grep nginx
[root@centos-6 nginx-1.9.4]# netstat -antup | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7715/nginx          
[root@centos-6 nginx-1.9.4]#

11)nginx维护命令

[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@centos-6 nginx-1.9.4]#
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s reload            //重新加载配置文而建(平滑重启)平滑重启(保持了C-S链接,不断开,服务器只是重新加载了配置文件,没有开启和关闭的服务器的一个动作)
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s stop             //停止Nginx,注意:启动没有任何参数
[root@centos-6 nginx-1.9.4]# echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local


12)查看nginx是否安装成功

源码编译安装LNMP



2、编译安装mysql

1)清理已安装的mysql

[root@centos-6 home]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@centos-6 home]# yum remove mysql-libs
[root@centos-6 home]# rpm -qa | grep mysql
[root@centos-6 home]#

2)安装依赖包

[root@centos-6 home]# yum install -y cmake ncurses-devel

3)编译安装mysql

[root@centos-6 lnmp]# tar xf mysql-5.6.26.tar.gz -C /usr/local/src/

3)创建运行mysql的帐号

[root@centos-6 mysql-5.6.26]# useradd -M -s /sbin/nologin mysql

4)配置编译参数

[root@centos-6 mysql-5.6.26]#cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_MEMORY_STORAGE_ENGINE=1\
-DWITH_READLINE=1\
-DENABLED_LOCAL_INFILE=1\
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL-USER=mysql

5)编译参数说明

DCMAKE_INSTALL_PREFIX                                      #制定mysql的安装根目录,目录在安装的时候会自动创建,这个值也可以在服务器启动时,用--basedir来设置
DMYSQL_UNIX_ADDR                                           #服务器与本地客户端进行通信的Unix套接字文件,必须是绝对路径,默认位置/tmp/mysql.sock,可以在服务器启动时,用--socket改变
DDEFAULT_CHARSET                        #mysql默认使用的字符集,不指定将默认使用Latin1西欧字符集
DDEFAULT_COLLATION                           #默认字符校对
DWITH_EXTRA_CHARSETS                  #制定mysql拓展字符集,默认值也是all支持所有的字符集
DWITH_MYISAM_STORAGE_ENGINE #静态编译MYISAM,INNOBASE,MEMORY存储引擎到MYSQL服务
                                   器,这样MYSQL就支持这三种存储引擎
DWITH_INNOBASE_STORAGE_ENGINE
DWITH_MEMORY_STORAGE_ENGINE
DWITH_READLINE                    #支持readline库
DENABLED_LOCAL_INFILE                    #允许本地倒入数据,启用加载本地数据
DMYSQL_DATADIR                                #mysql数据库存放路径
DMYSQL-USER                                    #运行mysql的用户

这些编译参数的帮助寻找方法:
http://www.mysql.com→→Documentation→→选择对应的版本(5.6)→→HTML Online→→View→→Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options→→
http://dev.mysql.com/doc/refman/ ... ration-options.html   


6)编译安装

[root@centos-6 mysql-5.6.26]#make -j 4 ; make install

        //以4个进程来加快编译速度,但不要超过CPU核心数,编译时间较长需耐心等待

7)配置mysql

[root@centos-6 mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/     //属主属组更改

[root@centos-6 mysql-5.6.26]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf       //覆盖原配置文件

[root@centos-6 mysql-5.6.26]#  cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld     //复制启动脚本

[root@centos-6 mysql-5.6.26]#  vim /etc/init.d/mysqld        //更改启动脚本中指定mysql位置

basedir=
datadir=
#修改为
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

[root@centos-6 mysql-5.6.26]# chkconfig mysqld  on        //设置开机启动


8)初始化数据库

[root@centos-6 mysql-5.6.26]#/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/etc/my.cnf \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data/ \
--user=mysql

查看初始化是否成功

[root@centos-6 mysql-5.6.26]#  ls /usr/local/mysql/data/
ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  test
[root@centos-6 mysql-5.6.26]#

[root@centos-6 mysql-5.6.26]# ln -s /usr/local/mysql/bin/* /bin/    ///这个里面是部分命令,让系统直接调用

[root@centos-6 mysql-5.6.26]# /etc/init.d/mysqld restart        //启动数据库

 ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!

[root@centos-6 mysql-5.6.26]# mysql_secure_installation    ////初始安全设置,设置root密码,急用root远程访问,删除test数据库等;

9)测试登陆是否正常

[root@centos-6 mysql-5.6.26]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.26 Source distribution

Copyright (c) 2000, 2015, 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

1)解决依赖关系

yum -y install php-pear

//pear按照一定的分类来管理pear应用代码库,你

的pear代码可以组织到其中适当的目录中,其他人可以方便的检索并分享到你的成果;pear不仅仅是一个代码仓库,它同时也是一个标准,使用这个标准来书写你的php代码,将会增强你的程序的可读性,复用性,减少出错的几率;Pear通过两个类为你搭建了一个框架,实现了诸如析构函数,错误捕获功能,你通过继承就可以使用这些功能


PHP添加libmcrypt拓展

       libmcrypt加密算法扩展库,支持DES, 3DES,RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法

       官方网站:http://mcrypt.hellug.gr/index.html  或者 http://mcrypt.sourceforge.net/

       在页面上点击下图连接,可以下载最新的2.5.8版本(页面上有个Browse All Files→→Libmcrypt)


源码编译安装LNMP


2)安装加密算法扩展库

[root@centos-6 lnmp]# tar xf libmcrypt-2.5.8.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/libmcrypt-2.5.8/

[root@centos-6 libmcrypt-2.5.8]#  ./configure --prefix=/usr/local/libmcrypt ; make -j 4 ; make install

3)除开上面的依赖解决之外,还需要安装图片,xml,字体支持基本库,使用yum去安装,安装的时候,这些软件包自身也有依赖!

[root@centos-6 libmcrypt-2.5.8]#  yum install -y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel libzip libzip-devel

4)需要添加到库文件路径

[root@centos-6 libmcrypt-2.5.8]# vim /etc/ld.so.conf

添加如下内容

include ld.so.conf.d/*.conf
/usr/local/libmcrypt/lib                       #此行添加
/usr/local/mysql/lib                           #此行添加

[root@centos-6 libmcrypt-2.5.8]#  ldconfig
[root@centos-6 libmcrypt-2.5.8]# echo 'ldconfig' >> /etc/rc.local

5)加压php

[root@centos-6 lnmp]#  tar xf php-5.6.13.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/php-5.6.13
[root@centos-6 php-5.6.13]#

6)配置php编译参数

[root@centos-6 php-5.6.13]#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --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-fpm --enable-mbstring --with-gd --enable-mysqlnd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --with-mcrypt=/usr/local/libmcrypt --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd

7)编译参数说明

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

--with-mysql                        #mysql安装目录,对mysql的支持

--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算法扩展

--with-zlib-dir=/usr/local/libzip   ##指定zip库路径


8)编译安装

[root@centos-6 php-5.6.13]# make -j 3 && make install


9)配置php和php-fpm

[root@centos-6 php-5.6.13]# cp /usr/local/src/php-5.6.13/php.ini-production /usr/local/php/php.ini        //替换php配置文件
[root@centos-6 php-5.6.13]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf        //替换php-fpm配置文件
[root@centos-6 php-5.6.13]# chown -R nginx:nginx /usr/local/php/etc/php-fpm.conf        //修改php-fpm.conf文件权限

[root@centos-6 nginx]# vim /usr/local/php/etc/php-fpm.conf        //修改php-fpm运行帐号为nginx

user = nobody

group = nobody

改为

user = nginx

group = nginx


10)配置php-fpm启动脚本并配置随机启动

[root@centos-6 php-5.6.13]# cp /usr/local/src/php-5.6.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos-6 php-5.6.13]#  chmod +x /etc/init.d/php-fpm
[root@centos-6 php-5.6.13]# chkconfig php-fpm on
[root@centos-6 php-5.6.13]#  /etc/init.d/php-fpm start
Starting php-fpm  done


11)测试php解释是否生效

[root@centos-6 php-5.6.13]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php

源码编译安装LNMP

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI