LNMP nginx+mysql+php 环境安装

一、LNMP介绍

LNMP 指的是 Linux 系统下 Nginx+MySQL+PHP 这种网站服务器架构。和 LAMP(httpd+mysql+php)不同的是,提供web服务的是 Nginx,并且 PHP 是作为一个独立服务存在的,这个服务叫做 php-fpm,Nginx 直接处理静态请求,动态请求会转发给 php-fpm。

Nginx应用场景:web服务、反向代理、负载均衡。

本文采用的软件版本:mysql-5.6.36、nginx-1.4.7、php-7.0.27

二、MySQL 安装

详情见 MySQL 二进制免编译安装

三、PHP 安装

和 LAMP 安装PHP方法有很大差别,需要开启 php-fpm 服务。

1)、安装

解压缩,创建 php-fpm 用户:

cd /usr/local/src
tar zxf php-7.0.27.tar.gz
useradd -s /sbin/nologin php-fpm

安装基础依赖:

yum install -y libjpeg libjpeg-devel epel-release libmcrypt-devel

配置编译选项:

cd php-7.0.27

./configure \
--prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--with-pear \
--with-curl  \
--with-openssl

如果在配置编译参数的过程中出现以下提示:

warning:configure: WARNING: unrecognized options: --with-mysql

这是安装的php版本不支持mysql模块所致,查看configure的正确语法:

./configure --help|grep 'mysql'

  --with-mysqli=FILE      Include MySQLi support.  FILE is the path
                          to mysql_config.  If no value or mysqlnd is passed
  --enable-embedded-mysqli
  --with-mysql-sock=SOCKPATH
  --with-pdo-mysql=DIR    PDO: MySQL support. DIR is the MySQL base directory
                          If no value or mysqlnd is passed as DIR, the
  --enable-mysqlnd        Enable mysqlnd explicitly, will be done implicitly
  --disable-mysqlnd-compression-support
                          Disable support for the MySQL compressed protocol in mysqlnd
  --with-zlib-dir=DIR     mysqlnd: Set the path to libz install prefix

在返回的信息中可以发现,的确没有--with-mysql,有的只是--with-pdo-mysql,并且已经集成到PHP的内核中,于是重新编译php支持mysql,改用

--with-pdo-mysql=/usr/local/mysql

如果提示:

configure: WARNING: unrecognized options: --with-mcrypt, --enable-gd-native-ttf

这是因为你安装的php版本不支持这两个选项,
把上面两个编译选项删除就可以了。

在phh7.1时,官方就开始建议用openssl系列函数代替Mcrypt系列的函数。

编译PHP

make

如果在编译的时候出现如下错误信息:

/usr/bin/ld: TSRM/.libs/TSRM.o: undefined reference to symbol 'pthread_sigmask@@GLIBC_2.2.5'
/usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

解决方法

vim Makefile
大概109行左右,在EXTRA_LIBS = 行-lcrypt后边添加 -lpthread

然后执行:

make clean && make

安装PHP:

make install

3)、PHP配置文件

复制配置文件:

cp php.ini-production /usr/local/php-fpm/etc/php.ini
vim /usr/local/php-fpm/etc/php-fpm.conf

# 修改为如下内容:
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

检查语法是否有错误:

/usr/local/php-fpm/sbin/php-fpm -t

如果返回的信息中含有test is successful,则表示无错误。

4)、启动脚本
复制启动脚本,并添加到开机启动:

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

检查是否启动成功:

service php-fpm start
ps aux |grep php-fpm

四、Nginx 安装

1)、安装

解压缩:

cd /usr/local/src
tar zxf nginx-1.4.7.tar.gz

编译安装:

cd nginx-1.4.7
./configure --prefix=/usr/local/nginx
make &&  make install

可能会出现如下错误信息:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

这是由于缺少相关基础依赖所致:

yum install -y pcre pcre-devel

2)、配置Nginx

1、编辑 Nginx 配置文件

首先清空原来的配置:

> /usr/local/nginx/conf/nginx.conf

vim !$

修改为如下内容:

# 定义Nginx运行的用户和用户组
user nobody nobody;

# Nginx进程数,建议设置为CPU的总核心数
worker_processes 2;

# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/nginx_error.log crit;

# 进程文件
pid /usr/local/nginx/logs/nginx.pid;

# 打开文件数,可以使用cat /proc/sys/fs/file-max命令查看系统可以打开的文件数。
worker_rlimit_nofile 51200;

# 工作模式与连接数上限
events
{
    # 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
    # epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll;

    # 单个进程最大连接数(最大连接数=连接数*进程数)该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置。
    worker_connections 6000;

    # 并发总数是 worker_processes 和 worker_connections 的乘积
    # 即 max_clients = worker_processes * worker_connections
    # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4
    # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:2 * 6000 = 12000
    # worker_connections 值的设置跟物理内存大小有关
    # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
    # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
    # 当前系统的内存是1G,可以打开的文件句柄数是
    # cat /proc/sys/fs/file-max
    # 98025
    # 12000 < 98025,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
    # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
    # 使得并发总数小于操作系统可以打开的最大文件数目,其实质也就是根据主机的物理CPU和内存进行配置
    # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
}

http
{
    # 文件扩展名与文件类型映射表
    include mime.types;

    # 默认文件类型
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;

    # 日志的格式
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

    # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
    # 防止网络阻塞
    tcp_nopush   on;

    # 防止网络阻塞
    tcp_nodelay  on;

    # 连接超时时间
    keepalive_timeout 30;

    client_header_timeout 3m;
    client_body_timeout 3m;

    # 用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;

    # 设定通过nginx上传文件的大小
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;

    # gzip模块设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

    # 虚拟主机配置
    #include vhosts/*.conf;
    # 或者直接在nginx.conf中直接定义:
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;

        location ~ \.php$
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    }
}

检查语法:

/usr/local/nginx/sbin/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

如果返回syntax is oktest is successful等信息则表示无错误。

2、编写 Nginx 启动脚本

vim /etc/init.d/nginx

修改为如下信息:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        restart
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

然后启动脚本的修改权限,并添加到开机启动:

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

启动服务,查看Nginx是否监听到端口:

/etc/init.d/nginx  start

netstat -lntp |grep 80

测试 PHP 解析:

vim /usr/local/nginx/html/1.php
<?php
    echo "test php scripts.";
?>
curl localhost/1.php

标签: web

评论已关闭