LNMP架构环境搭建之PHP、Nginx源码编译安装及其简单配置应用

LNMP架构中的Mysql见上一篇博文“LNMP架构环境搭建之mysql源码编译安装

一、PHP简介

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

二、PHP的源码安装

这里源码安装的PHP版本为5.6.35,使用./configure编译安装,PHP源码安装所需安装包有php-5.6.35.tar.bz2,gd-devel-2.0.35-11.el6.x86_64.rpm,re2c-0.13.5-1.el6.x86_64.rpm,libmcrypt-2.5.8-9.el6.x86_64.rpm,libmcrypt-devel-2.5.8-9.el6.x86_64.rpm,下载链接是(点击下载):PHP

  • 编译安装

[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm    libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  php-5.6.35.tar.bz2
gd-devel-2.0.35-11.el6.x86_64.rpm  mysql-5.7.11                            re2c-0.13.5-1.el6.x86_64.rpm
libmcrypt-2.5.8-9.el6.x86_64.rpm   mysql-boost-5.7.11.tar.gz
[root@server1 ~]# tar jxf php-5.6.35.tar.bz2   
##解压PHP源文件
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm    libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  php-5.6.35
gd-devel-2.0.35-11.el6.x86_64.rpm  mysql-5.7.11                            php-5.6.35.tar.bz2
libmcrypt-2.5.8-9.el6.x86_64.rpm   mysql-boost-5.7.11.tar.gz               re2c-0.13.5-1.el6.x86_64.rpm
  • 这里是我在编译安装过程中需要解决的依赖性
    [root@server1 ~]# yum install -y libxml2-devel openssl-devel curl-devel gd-devel-2.0.35-11.el6.x86_64.rpm gmp-devel libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm net-snmp-devel
    [root@server1 ~]# rpm -ivh re2c-0.13.5-1.el6.x86_64.rpm

  • 编译(添加参数 ./configure –help查看)
    [root@server1 ~]# cd php-5.6.35
    [root@server1 php-5.6.35]# ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-mysql=mysqlnd --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

    这里写图片描述[root@server1 php-5.6.35]# make && make install
    这里写图片描述

  • PHP简单配置

[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/etc
[root@server1 etc]# ll
total 28
-rw-r--r-- 1 root root  1288 Aug  5 12:47 pear.conf
-rw-r--r-- 1 root root 23089 Aug  5 12:46 php-fpm.conf.default
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default
[root@server1 etc]# 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 
 936 date.timezone = Asia/Shanghai   ##取消注释,并加上时区
[root@server1 php-5.6.35]# vim /usr/local/lnmp/php/etc/php-fpm.conf
 25 pid = run/php-fpm.pid   ##取消注释
 ##创建一个nginx用户,可以方便将后面的nginx和php结合使用
[root@server1 php-5.6.35]# id nginx
id: nginx: No such user
[root@server1 php-5.6.35]# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin nginx
[root@server1 php-5.6.35]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server1 php-5.6.35]# chmod +x /etc/init.d/php-fpm 
##测试脚本启动
[root@server1 php-5.6.35]# /etc/init.d/php-fpm start
Starting php-fpm  done
[root@server1 php-5.6.35]# /etc/init.d/php-fpm status
php-fpm (pid 16106) is running...
[root@server1 ~]# netstat -antlp | grep php
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name           
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      16106/php-fpm               

三、Nginx的源码安装

nginx 是一个高性能的web服务器,其主要的特点在于能够解决超高并发访问的问题,同时nginx也拥有负载均衡和反向代理的能力。这也是我们搭建lnmp的核心软件之一。
这里源码安装的Nginx版本为1.10.1,使用./configure编译安装,Nginx源码安装所需安装包有nginx-1.10.1.tar.gz,nginx-sticky-module-ng.tar.gz,下载链接是(点击下载):Nginx

  • 配置编译安装

[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm         mysql-5.7.11                   php-5.6.35
gd-devel-2.0.35-11.el6.x86_64.rpm       mysql-boost-5.7.11.tar.gz      php-5.6.35.tar.bz2
libmcrypt-2.5.8-9.el6.x86_64.rpm        nginx-1.10.1.tar.gz            re2c-0.13.5-1.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  nginx-sticky-module-ng.tar.gz
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz 
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz 
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm         mysql-boost-5.7.11.tar.gz      php-5.6.35
gd-devel-2.0.35-11.el6.x86_64.rpm       nginx-1.10.1                   php-5.6.35.tar.bz2
libmcrypt-2.5.8-9.el6.x86_64.rpm        nginx-1.10.1.tar.gz            re2c-0.13.5-1.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  nginx-sticky-module-ng
mysql-5.7.11                            nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# vim src/core/nginx.h
 14 #define NGINX_VER          "nginx"    ##删除版本号
[root@server1 nginx-1.10.1]# vim auto/cc/gcc 
178 # debug
179 #CFLAGS="$CFLAGS -g"    ##加上注释

[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --with-file-aio --with-threads

问题
这里写图片描述
解决
这里写图片描述

[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --with-file-aio --with-threads
[root@server1 nginx-1.10.1]# make && make install
这里写图片描述

  • 简单配置

[root@server1 nginx-1.10.1]# cd 
[root@server1 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf
 13     worker_connections  65535;

 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         }
[root@server1 ~]# vim /etc/security/limits.conf 
 51 nginx   -       nofile  65536
[root@server1 ~]# vim .bash_profile 
 10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin
[root@server1 ~]# source .bash_profile 
[root@server1 ~]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 ~]# nginx
[root@server1 ~]# netstat -antlp | grep nginx
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      19755/nginx                 
[root@server1 ~]# 

测试nginx
这里写图片描述

  • PHP与Nginx配合使用

[root@server1 ~]# vim /usr/local/lnmp/nginx/html/index.php
[root@server1 ~]# cat /usr/local/lnmp/nginx/html/index.php 
<?php
phpinfo()
?>
[root@server1 ~]# 

访问172.25.54.9/index.php

这里写图片描述

[root@server1 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf
##改变默认发布文档
 43         location / {
 44             root   html;
 45             index  index.php index.html index.htm;
 46         }
[root@server1 ~]# nginx -s reload
[root@server1 ~]#

访问172.25.54.9
这里写图片描述

应用——论坛搭建管理

首先下载论坛的压缩文件Discuz_X3.2_SC_UTF8.zip,下载地址:Discuz
然后我们就开始吧

[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm         mysql-5.7.11               nginx-sticky-module-ng.tar.gz
Discuz_X3.2_SC_UTF8.zip                 mysql-boost-5.7.11.tar.gz  php-5.6.35
gd-devel-2.0.35-11.el6.x86_64.rpm       nginx-1.10.1               php-5.6.35.tar.bz2
libmcrypt-2.5.8-9.el6.x86_64.rpm        nginx-1.10.1.tar.gz        re2c-0.13.5-1.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  nginx-sticky-module-ng
[root@server1 ~]# yum install -y unzip    ##下载zip压缩包的解压工具
[root@server1 ~]# unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
[root@server1 ~]# ls /usr/local/lnmp/nginx/html/
50x.html  index.html  index.php  readme  upload  utility
[root@server1 ~]# mv /usr/local/lnmp/nginx/html/upload/ /usr/local/lnmp/nginx/html/bbs
##更改一下目录的名称
[root@server1 ~]# ls /usr/local/lnmp/nginx/html/
50x.html  bbs  index.html  index.php  readme  utility
[root@server1 ~]# 

这里写图片描述
这里写图片描述

[root@server1 ~]# cd /usr/local/lnmp/nginx/html/bbs/
[root@server1 bbs]# chmod 777 config/ data/ uc_client/ uc_server/ -R
[root@server1 bbs]# 

这里写图片描述
这里写图片描述

##开启数据库
[root@server1 bbs]# mysql -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/lnmp/mysql/data/mysql.sock' (111)
[root@server1 bbs]# /etc/init.d/mysqld status
 ERROR! MySQL is not running, but PID file exists
[root@server1 bbs]# /etc/init.d/mysqld start
Starting MySQL SUCCESS! 
[root@server1 bbs]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11 Source distribution

Copyright (c) 2000, 2016, 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> quit
Bye
[root@server1 bbs]#

这里写图片描述
这里写图片描述

root@server1 bbs]# vim /usr/local/lnmp/php/etc/php.ini 
1013 pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock
1162 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
1221 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
[root@server1 bbs]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@server1 bbs]# 

这里写图片描述

[root@server1 bbs]# cd
[root@server1 ~]# ll -d /usr/local/lnmp/mysql/data/
drwxr-x---  5 mysql root  4096 Aug  5 15:26 data
[root@server1 ~]# chmod 755 /usr/local/lnmp/mysql/data/
[root@server1 ~]# ll -d /usr/local/lnmp/mysql/data/
drwxr-xr-x  5 mysql root  4096 Aug  5 15:26 data
[root@server1 ~]#

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
[root@server1 ~]# rm -f /usr/local/lnmp/nginx/html/bbs/install/index.php
这里写图片描述

PHP扩展——mencache缓存模块

首先下载memcache的压缩文件memcache-2.2.5.tgz,下载地址:Memcache

[root@server1 ~]# tar zxf memcache-2.2.5.tgz 
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm         memcache-2.2.5.tgz         nginx-sticky-module-ng.tar.gz
Discuz_X3.2_SC_UTF8.zip                 mysql-5.7.11               package.xml
gd-devel-2.0.35-11.el6.x86_64.rpm       mysql-boost-5.7.11.tar.gz  php-5.6.35
libmcrypt-2.5.8-9.el6.x86_64.rpm        nginx-1.10.1               php-5.6.35.tar.bz2
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  nginx-1.10.1.tar.gz        re2c-0.13.5-1.el6.x86_64.rpm
memcache-2.2.5                          nginx-sticky-module-ng
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# ls
config9.m4  CREDITS      memcache_consistent_hash.c  memcache_queue.c    memcache_standard_hash.c
config.m4   example.php  memcache.dsp                memcache_queue.h    php_memcache.h
config.w32  memcache.c   memcache.php                memcache_session.c  README
[root@server1 memcache-2.2.5]# vim ~/.bash_profile 
 10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin:/usr/local/lnmp/php/bin
[root@server1 memcache-2.2.5]# source ~/.bash_profile  
[root@server1 memcache-2.2.5]# phpize 
##phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@server1 memcache-2.2.5]# ls
acinclude.m4    config.h.in   CREDITS          memcache_consistent_hash.c  memcache_standard_hash.c
aclocal.m4      config.m4     example.php      memcache.dsp                missing
autom4te.cache  config.sub    install-sh       memcache.php                mkinstalldirs
build           configure     ltmain.sh        memcache_queue.c            php_memcache.h
config9.m4      configure.in  Makefile.global  memcache_queue.h            README
config.guess    config.w32    memcache.c       memcache_session.c          run-tests.php
[root@server1 memcache-2.2.5]# ./configure 
[root@server1 memcache-2.2.5]# make && make install
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 no-debug-non-zts-20131226]# ls
memcache.so  opcache.a  opcache.so
[root@server1 no-debug-non-zts-20131226]# pwd
/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226
[root@server1 no-debug-non-zts-20131226]# php -m | grep memcache
[root@server1 no-debug-non-zts-20131226]# vim /usr/local/lnmp/php/etc/php.ini 
 873 extension=memcache.so
[root@server1 no-debug-non-zts-20131226]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@server1 no-debug-non-zts-20131226]# php -m | grep memcache
memcache
[root@server1 no-debug-non-zts-20131226]# cd
[root@server1 ~]# yum install -y memcached
[root@server1 ~]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]
[root@server1 ~]# netstat -antlp | grep memcached
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      22636/memcached     
tcp        0      0 :::11211                    :::*                        LISTEN      22636/memcached     
[root@server1 ~]# cp memcache-2.2.5/memcache.php /usr/local/lnmp/nginx/html/
[root@server1 ~]# cp memcache-2.2.5/example.php /usr/local/lnmp/nginx/html/
[root@server1 ~]# vim /usr/local/lnmp/nginx/html/memcache.php 
 23 define('ADMIN_PASSWORD','redhat');      // Admin Password
 28 $MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

Openresty与Nginx简单配置

首先下载openresty的压缩文件openresty-1.13.6.1.tar.gz,下载地址:Openresty

[root@server1 ~]# nginx -s stop         ##将先前开启的nginx停掉                             
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz 
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm         mysql-5.7.11                   openresty-1.13.6.1.tar.gz
Discuz_X3.2_SC_UTF8.zip                 mysql-boost-5.7.11.tar.gz      package.xml
gd-devel-2.0.35-11.el6.x86_64.rpm       nginx-1.10.1                   php-5.6.35
libmcrypt-2.5.8-9.el6.x86_64.rpm        nginx-1.10.1.tar.gz            php-5.6.35.tar.bz2
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm  nginx-sticky-module-ng         re2c-0.13.5-1.el6.x86_64.rpm
memcache-2.2.5                          nginx-sticky-module-ng.tar.gz
memcache-2.2.5.tgz                      openresty-1.13.6.1
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure --prefix=/usr/local/lnmp/openresty --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --with-file-aio --with-threads
[root@server1 openresty-1.13.6.1]# gmake && gmake install
[root@server1 openresty-1.13.6.1]# cd /usr/local/lnmp/openresty/nginx/
[root@server1 nginx]# ls
conf  html  logs  sbin
[root@server1 nginx]# vim conf/nginx.conf
[root@server1 nginx]#
 19         upstream memcache {
 20         server localhost:11211;
 21         keepalive 512;
 22         }
 49         location / {
 50             root   html;
 51             index  index.php index.html index.htm;
 52         }
 53         location /memc {
 54         internal;
 55         memc_connect_timeout 100ms;
 56         memc_send_timeout 100ms;
 57         memc_read_timeout 100ms;
 58         set $memc_key $query_string;
 59         set $memc_exptime 300;
 60         memc_pass memcache;
 61         }
 80         location ~ \.php$ {
 81             root           html;
 82             fastcgi_pass   127.0.0.1:9000;
 83             fastcgi_index  index.php;
 84             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 85             include        fastcgi.conf;
 86         }
[root@server1 nginx]# /usr/local/lnmp/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# /usr/local/lnmp/openresty/nginx/sbin/nginx
[root@server1 nginx]#

这里写图片描述

[root@server1 nginx]# cd html/
[root@server1 html]# pwd
/usr/local/lnmp/openresty/nginx/html
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# 

这里写图片描述
这里写图片描述

[root@foundation54 lamp]# ab -c 10 -n 1000 http://172.25.54.9/index.php
##其他略掉
Requests per second:    428.55 [#/sec] (mean)
[root@foundation54 lamp]# ab -c 10 -n 1000 http://172.25.54.9/example.php
Requests per second:    1001.07 [#/sec] (mean)
[root@foundation54 lamp]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值