LNMP环境搭建

  • A+
所属分类:Linux

安装MySQL

请参考LAMP的操作,此处不多加赘述

安装PHP

Nginx的PHP安装和LAMP环境搭建中的PHP安装是有区别的。因为Nginx中的PHP是以fastcgi的方式结合Nginx的,而httpd是把PHP作为自己的模块来调用的。

我这里还是使用LAMP中的源代码地址进行下载的。其中下载和解压的过程请参考LAMP环境搭建的命令。

创建账号,来运行php-fpm服务

# useradd -s /sbin/nologin php-fpm

进行编译,此处和LAMP环境搭建是有区别的,多了--enable-fpm

./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-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 \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl 

执行上述命令可能会出现下面的报错:

错误1:configure: error: Please reinstall the libcurl distribution -    easy.h should be in <curl-dir>/include/curl/

解决办法:

# yum install -y libcurl-devel

重新编译:make

错误2:/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] 错误 1

解决办法:

# vim Makefile搜索-lcrypt在其后面加上 -lpthread
错误3:collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] 错误 1

解决办法:

# make clean && make

问题到这里就可以编译成功进行安装了!

# make install

修改配置文件:

# 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,代表配置正确

[03-Sep-2018 15:46:38] NOTICE: configuration file 
/usr/local/php-fpm/etc/php-fpm.conf test is successful

启动php-fpm:

# cp /usr/local/src/php-5.6.32/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod 755 /etc/init.d/php-fpm 
# service php-fpm start

出现下面字样代表成功!

Starting php-fpm  done

设置开机启动

# chkconfig php-fpm on

查看php-fpm是否启动的命令如下:

# ps aux | grep php-fpm

下载Nginx源码包并解压:

# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.8.0.tar.gz
# tar zxvf nginx-1.8.0.tar.gz

配置编译选项并安装:

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

编写Nginx的启动脚本,并加入系统服务:

# vi /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.conf的配置文件清空,并重新写入:

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

# vim /usr/local/nginx/conf/nginx.conf

//配置文件内容:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

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 on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    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;
    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 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;

    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

显示test is successful代表配置正确

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

开启Nginx服务

//注意:apache服务不可同时打开,否则有报错
# service nginx start

正确开启服务,出现的现象:

# service nginx start            
Starting nginx (via systemctl):                            [  确定  ]

确认Nginx的状态命令:

# systemctl status nginx

创建测试文件

# vim /usr/local/nginx/html/2.php

<?php

        echo "test php scripts.\n"
?>

检验是否解析正常:

 # curl localhost/2.php           
 test php scripts.

Nginx配置

默认虚拟主机

# vi /usr/local/nginx/conf/nginx.conf

到最后一个大括号}前添加一条命令:
include vhost/*.conf;

//上面那条命令是为了加载在vhost目录以conf结尾的配置文件
# mkdir /usr/local/nginx/conf/vhost

# cd /usr/local/nginx/conf/vhost

# vim default.conf

//配置文件内容

server
{
    listen 80 default_server;  //有这个标记的就是默认虚拟主机
    server_name liutest.com;
    index index.html index.htm index.php;
    root /data/nginx/default;
}

检验是否配置正确:

# /usr/local/nginx/sbin/nginx -t

出现test is successful代表配置正确。

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

重启Nginx服务:

# /usr/local/nginx/sbin/nginx -s reload

创建default目录:

# mkdir -p /data/nginx/default

创建索引页:

# echo "default_server" > /data/nginx/default/index.html

检验解析是否正常:

# curl -x127.0.0.1:80 liutest.com
default_server

创建一个新的虚拟主机:

# cd /usr/local/nginx/conf/vhost/
# vim test.com.conf

//配置文件内容
server
    {
        listen 80;
        server_name test.com;
        index index.html index.htm index.php;
        root /data/nginx/test.com;

        location / 
        {
            auth_basic              "Auth";
			auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
        }    
    }

配置文件中auth_basic是为了打开认证,auth_basic_user_file指定用户密码文件

安装过httpd,如果之前安装了apache 2.4可不用安装

# yum install -y httpd

创建用户liu

# htpasswd -c /usr/local/nginx/conf/htpasswd liu

New password: 
Re-type new password: 
Adding password for user liu

检验是否配置正确:

# /usr/local/nginx/sbin/nginx -t

出现下面test is successful代表配置正确。

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

重启Nginx服务:

# /usr/local/nginx/sbin/nginx -s reload

使用curl命令进行验证用户认证配置:

# mkdir /data/nginx/test.com
# echo "test.com" > /data/nginx/test.com/index.html
# curl -I -x127.0.0.1:80 test.com

出现下面401 Unauthorized字样代表成功。如果你想看到图形化界面的认证方式,可以用能与之通信的window系统,配置好hosts文件,访问test.com即可看到效果。若此处不知道如何配置,可留言。

HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Mon, 03 Sep 2018 12:00:15 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

由于篇幅的原因就到这里了,以后有时间再分享Nginx的更多配置:域名重定向、Nginx的访问日志、防盗链、访问控制、Nginx代理、SSL配置等等。

  • 我的微信
  • 欢迎大家,与我交流,非诚勿扰谢谢
  • weinxin
  • 奕知伴解 微信公众号
  • 扫一扫关注,从此不迷路。
  • weinxin
刘銮奕

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: