安装下基本的所需组件:

yum groupinstall “Development Tools” “Additional Development”

一、Nginx的安装:

下载nginx.tar.gz包, 过程省略!

安装pcre扩展包:

yum -y install pcre-devel

添加nginx用户和nginx组:

groupadd -r nginx

useradd -r -g nginx nginx

编译配置nginx:

./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre

最后:

make && make install

为nginx添加启动脚本文件, 编辑/etc/init.d/nginx, 添加如下内容:

#!/bin/sh
#
#nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

为脚本赋予执行权限:

chmod +x /etc/init.d/nginx

添加至服务管理列表,并让其开机自动启动:

chkconfig –add nginx

chkconfig nginx on

而后就可以启动服务并测试了:

service nginx start

二、编译安装mysql5.6.32

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

tar xf mysql-5.6.32.tar.gz

cd mysql-5.6.32

mkdir bld

cd bld

cmake ..

make

make install

cd /usr/local/mysql

chown -R mysql .

chgrp -R mysql .

scripts/mysql_install_db –user=mysql

chown -R root .

chown -R mysql data

bin/mysqld_safe –user=mysql &

cp support-files/mysql.server /etc/init.d/mysqld

chkconfig –add mysqld

chkconfig mysqld on

cp support-files/my-default.cnf /etc/my.cnf

vim /etc/profile.d/mysqld, 添加如下内容:

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

vim /etc/man.config, 在最后一行添加如下内容:

MANPATH /usr/local/mysql/man

输出mysql的头文件至系统头文件路径/usr/include,如下:

ln -sv /usr/local/mysql/include /usr/include/mysql

输出mysql的库文件给系统库查找路径:

echo ‘/usr/local/mysql/lib’ > /etc/ld.so.conf.d/mysql.conf

而后让系统重新载入系统库:

ldconfig

最后, 重新启动测试下效果:

如果报错pid文件不存在,可以尝试以下方案:

尝试方案:

chown -R mysql:mysql /usr/local/mysql/data

chmod -R 755 /usr/local/mysql/data

终极方案:

清空mysql数据目录下所有内容:

rm -rf data/*

重新初始化:

scripts/mysql_install_db –user=mysql

三、编译安装php5.6.24

yum install -y libmcrypt libmcrypt-devel

tar xf php-5.6.24.tar.gz

cd php-5.6.24

./configure \
 --prefix=/usr/local/php \
 --with-mysql=/usr/local/mysql \
 --with-openssl \
 --enable-fpm \
 --enable-sockets \
 --enable-sysvshm \
 --with-mysqli=/usr/local/mysql/bin/mysql_config  \
 --enable-mbstring \
 --with-freetype-dir \
 --with-jpeg-dir \
 --with-png-dir \
 --with-zlib-dir \
 --with-libxml-dir=/usr \
 --enable-xml \
 --with-mhash \
 --with-mcrypt \
 --with-config-file-path=/etc \
 --with-config-file-scan-dir=/etc/php.d \
 --with-bz2 \
 --with-curl

make && make install

为php提供配置文件:

cp php.ini-production /etc/php.ini

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

chkconfig –add php-fpm

chkconfig php-fpm on

为php-fpm提供配置文件:

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

编辑php-fpm的配置文件:

vim /usr/local/php/etc/php-fpm.conf

配置fpm的相关选项为你所需要的值:

pm.max_children = 150

pm.start_servers = 8

pm.min_spare_servers = 5

pm.max_spare_servers = 10

接下来就可以启动php-fpm了:

service php-fpm start

四、整合nginx和php

1、编辑/etc/nginx/nginx.conf,启用如下选项:

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;
}

2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:

提示:快速清空内容: >/etc/nginx/fastcgi_params

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

 

并在所支持的主页面格式中添加php格式的主页,类似如下:

location / {
    root html;
    index index.php index.html index.htm;
}

而后重新载入nginx的配置文件:

service nginx reload

3、在/usr/html新建index.php的测试页面,测试php是否能正常工作:

vim /usr/html/index.php

<?php

phpinfo();

五、配置nginx的虚拟主机,并且都支持PHP

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

cd /etc/nginx

创建额外配置文件目录:

mkdir conf.d

编辑主配置文件:

vim nginx.conf

在http代码块中引入附加的配置文件, 类似:

include conf.d/vhosts.conf; #引入额外配置文件

include mime.types;

编辑额外的配置虚拟主机配置文件:

vim /etc/nginx/conf.d/vhosts.conf

添加内容如下:

server{
    listen 80;
    server_name lnmp.server;
    charset utf-8;
    index index.php index.html index.htm;
    root /www/lnmp.server;
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
        include fastcgi_params;
    }

}

 

创建虚拟主机根目录:

mkdir -p /www/lnmp.server

添加测试文件:

vim /www/lnmp.server/index.php

内容如下:

<?php

phpinfo();

 

编辑本地hosts文件, vim /etc/hosts, 添加一行:

127.0.0.1 lnmp.server

最后测试:

curl lnmp.server

安装完成!有遗漏的欢迎联系我的微信号wikitest, 或直接下方留言!