专栏首页奕知伴解LNMP环境搭建

LNMP环境搭建

安装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 SettingsNGINX_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
esacexit $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.comdefault_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 liuNew 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配置等等。

如果手机观看不舒服,可用电脑访问www.llyit.cn查看

本文分享自微信公众号 - 奕知伴解(yzbjchat),作者:小奕

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-09-03

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Windows server 2012 AD 证书 服务搭建

    刘銮奕
  • Centos7安装python3以及Scapy3介绍

    Redhat:主要是服务器型Linux,商用收费;RHEL是Red Hat Enterprise Linux的缩写。

    刘銮奕
  • LNMP环境下Nginx配置

    在早期的Linux服务器上,一个服务器只能运行一个网站,也就是只能跑一个域名。但随着技术的发展,一个服务器上可以跑多个域名了,这样可以帮我们节省了成本。其实这里...

    刘銮奕
  • LNMP环境下Nginx中php-fpm的配置文件讲解

    老七Linux
  • LNMP搭建多个虚拟主机(wordpress+discuz+dedecms)

    老七Linux
  • linux 中php以及nginx的重启命令

    关闭php killall php-fpm 注意:php关闭之前,使用ps aux|grep 'php' 查看一下php的服务及进程 php重启 /usr/lo...

    joshua317
  • LNMP_nginx安装_3

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

    py3study
  • 02.docker镜像制作

    陈雷雷
  • Nginx(3)——支持HTTPS

    在configure arguments:后面显示的原有的configure参数,不支持ssl模块,需要增加ssl 模块

    羊羽shine
  • 高并发调优backlog多大合适?

    那么对于nginx,对于php-fpm,backlog应该设置多大,是越大越好吗?backlog怎么设置合适?这是上篇文章中遗留的几个问题

    李俊鹏

扫码关注云+社区

领取腾讯云代金券