[linux下php mysql nginx环境变量配置]Linux下php+mysql+nginx环境编译搭建教程

更新时间:2020-03-28    来源:linux    手机版     字体:

【www.bbyears.com--linux】

1、 nginx

版本:1.5

下载地址: http://nginx.org/download/nginx-1.5.2.tar.gz

2、 mysql

版本5.5

下载地址:http://downloads.mysql.com/archives/mysql-5.0/mysql-5.5.30.tar.gz

3、 php

版本5.4

下载地址:http://am1.php.net/get/php-5.4.34.tar.gz/from/this/mirror

一:安装nginx

安装一些依赖包:

yum -y install gcc gcc-c++ gcc-devel gcc-c++-devel ssl ssl-devel autoconf make aclocal libtool expat-devel libxml2-devel openssl openssl-devel zlib zlib-devel bzip2 bzip2-devel gd gd-devel libmcrypt libmcrypt-devel libXpm-devel  curl-devel libgd-devel gd-devel openldap-devel
进入一个目录:

cd /opt/
下载并解压:

wget http://nginx.org/download/nginx-1.5.2.tar.gz
tar -zxf nginx-1.5.2.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar -zxf ngx_cache_purge-2.1.tar.gz
wget http://sourceforge.net/projects/pcre/files/pcre/8.34/pcre-8.34.tar.gz
tar -zxf pcre-8.34.tar.gz

进入目录并编译:

cd nginx-1.5.2
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --http-log-path=/usr/local/nginx/log/access.log --pid-path=/usr/local/nginx/run/nginx.pid    --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --add-module=../ngx_cache_purge-2.1 --with-pcre=../pcre-8.34
make  && make install

这样就完成了nginx的搭建。
运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] getpwnam("www") failed
在nginx.conf中 把user nobody的注释去掉既可,改成www
再次运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] getpwnam("www") failed in /usr/local/nginx/conf/nginx.conf:1
错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www,如下命令:

groupadd -f www
useradd -g www www

第三次运行:/usr/local/nginx/sbin/nginx
报错:nginx: [emerg] mkdir() "/usr/local/nginx/tmp/client" failed (2: No such file or directory)
执行:mkdir -p /usr/local/nginx/tmp/client
然后localhost访问就可以看到:

 

习惯了了/etc/init.d/nginx start?觉得/usr/local/nginx/sbin/nginx 太长?
vim /etc/init.d/nginx

#!/bin/bash
#
# Init file for nginx server daemon
#
# chkconfig: 234 99 99
# description: nginx server daemon
#
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
RETVAL=0
prog="nginx"
PAT=/usr/local/nginx
NGINXD=/usr/local/nginx/sbin/nginx
PID_FILE=/usr/local/nginx/nginx.pid
start()
{
echo -n $"Starting $prog: "
$NGINXD 2>/dev/null $OPTIONS && success || failure
RETVAL=$?
[ "$RETVAL" = 0 ] && touch /var/lock/subsys/nginx
echo
}
stop()
{
echo -n $"Shutting down $prog: "
killproc nginx
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/nginx
return $RETVAL
}
reload()
{
echo -n $"Reloading nginx: "
killproc nginx -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
stop
start
;;
"reload")
reload
;;
"status")
status -p $PID_FILE nginx
RETVAL=$?
;;
*)
 
echo $"Usage: $0 {start|stop|restart|reload|status}"
RETVAL=1
esac
exit $RETVAL
保存,添加x权限。
如需开机启动:
chkconfig nginx on


二、安装mysql

进入安装目录:

cd /opt

下载并解压:

wget http://downloads.mysql.com/archives/mysql-5.0/mysql-5.5.30.tar.gz
tar -zxf mysql-5.5.30.tar.gz
编译安装:

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/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
 
make && make install

如果报错:

rm -f CMakeCache.txt
yum -y install ncurses-devel

添加用户并修改权限:

groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql

进入安装路径并执行初始化配置脚本,创建系统自带的数据库和表

cd /usr/local/mysql
scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

添加服务,拷贝服务脚本到init.d目录,并设置开机启动:

cp support-files/mysql.server /etc/init.d/mysql
cp support-files/my-medium.cnf /etc/my.cnf #copy conf
chkconfig mysql on
service mysql start  --启动MySQL

设置PATH,支持直接调用mysql(修改/etc/profile文件,在文件末尾添加):

PATH=/usr/local/mysql/bin:$PATH
export PATH

关闭文件,运行下面的命令,让配置立即生效

source /etc/profile

另:mysql5.5开始支持cmake。至于两者的差别:
./configure就是执行你当前目录下一个名叫configure的脚本,由它生成Makefile,有了Makefile之后,一般来说就可以通过make进行编译,make install进行安装.
cmake就是一个与make同级别的编译工具,只不过它依靠的不是Makefile作为编译规则,而是根据CMakeLists.txt来编译的。

Cmake 对照:http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html

另:Cmeke如果没有 yum install cmake,如果还没有请前往:http://www.cmake.org/download


三、安装php

进入安装目录:

cd /opt
下载并解压:

wget  http://am1.php.net/get/php-5.4.34.tar.gz/from/this/mirror
tar -zxf php-5.4.34.tar.gz

进入目录编译:

cd php-5.4.34
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml  --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/usr/local/mcrypt --with-gd  --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-pear   --enable-pdo --with-pdo-mysql --with-gettext --enable-exif --enable-wddx --enable-calendar --enable-ftp  --enable-dba --enable-sysvmsg  --enable-sysvshm --enable-debug --enable-maintainer-zts --enable-embed --with-pcre-regex --enable-gd-jis-conv --with-fpm-user=www --with-fpm-group=www --enable-sockets
报错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.

在http://sourceforge.net/projects/mcrypt/files/下载libmcrypt和mcrypt编译(其中编译mcrypt需要mhash的支持,所以编译完libmcrypt后需要下载mhash,编译完mhash后才编译mcrypt)

wget  http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
tar -zxvf libmcrypt-2.5.8.tar.gz
cd  libmcrypt-2.5.8
./configure
make && make install
 
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
tar -zxvf mhash-0.9.9.9.tar.gz
cd  mhash-0.9.9.9
./configure
make && make install
 
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
tar -zxvf mcrypt-2.6.8.tar.gz
cd  mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure
make && make install

此时再编译,通过

[ 如果报configure: error: Cannot find ldap libraries in /usr/lib
解决办法:cp -frp /usr/lib64/libldap* /usr/lib/ ]

make && make install

由于php5.4已经集成了php-fpm,所以我们不需要再额外下载php-fpm。
直接运行:

/usr/local/php/sbin/php-fpm
报错:ERROR: failed to open configuration file "/usr/local/php/etc/php-fpm.conf": No such file or directory (2)

cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
解决。ps aux | grep php 查看,已经启动。
另外php的配置文件复制一份:cp /opt/php-5.4.34/php.ini-production /usr/local/php/etc/php.ini

此时新建一个php文件访问,是不是还不支持?哈[坏笑]。
修改nginx.conf

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
 }

重启nginx,至此,所有完结。

然后是不是又有点淡淡的忧伤?觉得/usr/local/php/sbin/php-fpm太长?习惯了/etc/init.d/php-fpm start?
vim /etc/init.d/php-fpm

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php
exec_prefix=${prefix}
 
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        "created")
                        if [ -f "$2" ] ; then
                                try=""
                                break
                        fi
                        ;;
                        "removed")
                        if [ ! -f "$2" ] ; then
                                try=""
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN --daemonize $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-quit"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac
保存,添加x权限。

如需开机启动:chkconfig php-fpm on

补充:设置FPM线程数量

PHP5.4安装完毕后,FPM的默认配置文件位于/usr/local/php/etc/php-fpm.conf.default
>cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
>vim /usr/local/php/etc/php-fpm.conf
输入”/www”,搜索www所在的POOL
pm = dynamic
 
; The number of child processes to be created when pm is set to "static" and the
; maximum number of child processes when pm is set to "dynamic" or "ondemand".
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don"t
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to "static", "dynamic" or "ondemand"
; Note: This value is mandatory.
pm.max_children = 5
 
; The number of child processes created on startup.
; Note: Used only when pm is set to "dynamic"
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 2
 
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to "dynamic"
; Note: Mandatory when pm is set to "dynamic"
pm.min_spare_servers = 1
 
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to "dynamic"
; Note: Mandatory when pm is set to "dynamic"
pm.max_spare_servers = 3
 
; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to "ondemand"
; Default Value: 10s
;pm.process_idle_timeout = 10s;
 
; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify "0". Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500
解释一下:
pm = dynamic 如何控制子进程,选项有static和dynamic,默认采用dynamic;如果选择static,则由pm.max_children指定固定的子进程数。
如果选择dynamic,则由以下参数决定:
pm.max_children ,子进程最大数
pm.start_servers ,启动时的进程数
pm.min_spare_servers ,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers ,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
对于专用服务器,pm可以设置为static。
pm.max_requests 设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 设置为500就可以了(默认0)。

将值修改为如下:
pm.max_children = 32
pm.start_servers = 16
pm.min_spare_servers = 8
pm.max_spare_servers = 32
pm.max_requests = 500

:wq 保存退出VIM

>/usr/local/php/sbin/php-fpm -t
NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
测试配置文件是否正常,没问题,杀掉当前的FPM进程
>/usr/local/php/sbin/php-fpm启动

本文来源:http://www.bbyears.com/caozuoxitong/90252.html