Nginx编译安装

  1. 下载、解压Nginx

cd /usr/local/src

wget http://nginx.org/download /nginx-1.8.0.tar.gz

tar -zxvf nginx-1.8.0.tar.gz
2. 编译安装Nginx

cd /usr/local/src/nginx-1.8.1
配置编译参数

./configure \

–prefix=/usr/local/nginx \

–with-http_realip_module \

–with-http_sub_module \

–with-http_gzip_static_module \

–with-http_stub_status_module \

–with-pcre
配置解释:

–prefix=/usr/local/nginx \
配置Nginx的安装目录

–with-http_realip_module \
允许ngx_http_realip_module模块(mod_rpaf)

此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。

–with-http_sub_module \
允许ngx_http_sub_module模块

这个模块可以能够在nginx的应答中搜索并替换文本。

–with-http_gzip_static_module \
允许ngx_http_gzip_static_module模块(mod_dflate)。

这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。

–with-http_stub_status_module \
允许ngx_http_stub_status_module模块(mod_status)

这个模块可以取得一些nginx的运行状态,如果是工业状况,可以直接取消。

编译、安装

make && make install
3. 启动并检测是否启动成功

/usr/local/nginx/sbin/nginx
ps aux | grep nginx
4. 配置Ngix配置文件

vim /usr/local/nginx/conf/nginx.conf
找到

location = /50x.html {

root html;

}
在下面加入以下内容

location ~ .php$ {

root html;

fastcgi_pass unix:/tmp/php-fcgi.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

include fastcgi_params;

}
上面这段配置的意思就是让所有的PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置

  1. 测试PHP解析

vim /usr/local/nginx/html/2.php
写入以下内容

!/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 " S t a r t i n g prog: “

mkdir -p /dev/shm/nginx_temp

daemon NGINXSBINc N G I N X S B I N − c NGINX_CONF

RETVAL=$?

echo

return $RETVAL

}

stop() {

echo -n "Stopping " S t o p p i n g prog: “

killproc -p NGINXPID N G I N X P I D NGINX_SBIN -TERM

rm -rf /dev/shm/nginx_temp

RETVAL=$?

echo

return $RETVAL

}

reload(){

echo -n "Reloading " R e l o a d i n g prog: “

killproc -p NGINXPID N G I N X P I D NGINX_SBIN -HUP

RETVAL=$?

echo

return $RETVAL

}

restart(){

stop

start

}

configtest(){

NGINXSBINc N G I N X S B I N − c NGINX_CONF -t

return 0

}

case “$1” in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

restart

;;

configtest)

configtest

;;

*)

echo "Usage: " U s a g e : 0 {start|stop|reload|restart|configtest}”

RETVAL=1

esac

exit $RETVAL
这个启动脚本我真的无能为力啊,没学过shell编程…

赋予启动文件权限755

chmod 755 /etc/init.d/nginx
7. Nginx服务开机启动

chkconfig –add nginx

chkconfig nginx on

服务启动、停止、重启的方法:

service nginx start 或 /etc/init.d/nginx start

service nginx stop 或 /etc/init.d/nginx stop

service nginx restart 或 /etc/init.d/nginx restart

  1. 更改nginx的配置,配置虚拟主机

首先将原来的配置文件清空,但是我们最好先备份一下

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

/usr/local/nginx/conf/nginx.conf
然后编辑文件并写入以下内容

vim /usr/local/nginx/cong/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 ‘ remoteaddr r e m o t e a d d r http_x_forwarded_for [$time_local]’

host" h o s t " request_uri” $status’

‘” httpreferer"" h t t p r e f e r e r "" 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;

}

}

include /usr/local/nginx/conf/vhosts/*.conf;

}
上面的配置主要就是开启了其他的一些功能,但是最主要的还是最后一句,配置了虚拟主机的配置文件的位置。

另外注意一点,这次给nginx指定了用户和用户组都是nobody

创建虚拟主机配置文件的目录和文件

mkdir /usr/local/nginx/conf/vhosts

cd !$

vim default.conf
创建的是一个默认虚拟主机,所以配置文件中用default_server字段来标识

配置文件如下:

server{

listen 80 default_server;

server_name www.liuke.com;

index index.html index.htm index.php;

root /tmp/tmp;

�deny all;

}
大致意思就是说要监听80端口,而且是个默认虚拟主机(也就是说无论什么域名只要指向到这台机器都会访问到这个虚拟主机)

但是配置文件的最后谢了一句deny all;代表禁止访问,所以,不匹配域名的所有访问都会被禁止

  1. 更改php-fpm的配置

vim /usr/local/php/etc/php-fpm.pid
更改配置如下:

pid = /usr/local/php/var/run/php-fpm.pid

error_log = /usr/local/php/var/log/php-fpm.log

[www]

listen = /tmp/php-fcgi.sock

user = php-fpm

group = php-fpm

listen.owner = nobody //和后面的nginx的一致

listen.group = nobody //同上

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
其实和之前配置的差别就是指定了socket文件的监听用户和监听用户组都是nobody,这和之前我让大家注意的地方(nginx新的配置文件中也增加了用户和用户组)是前后呼应的,因为nginx和php-fpm要进行通信,所以要对socket文件具有读写权,其他人对这个文件不能有任何其他的权限这才是安全的,所以我们让nginx的用户和用户组都是noboody,php-fpm对于socket文件的监听用户也是nobody,这样就保证了socket文件的可读写性和系统的安全性

但是我有个疑问:为什么不直接让php-fpm的用户和用户组直接也是nobody呢,这样就不用费事的去为php-fpm专门建立一个用户,为socket文件的监听者指定用户了???

重启php-fpm服务

service php-fpm restart
然后我们可以看到的是,/tmp/php-fcgi.sock文件的所有者就变成了nobody,用户和用户组拥有读写权,其他权限都没有

  1. 测试php解析

curl localhost/2.php

错误集锦:

问题1:

error: the HTTP rewrite module requires the PCRE library.

yum install -y pcre-devel

问题2:

出现端口占用的情况,刚有个同学问我端口冲突了,nginx启动不了,其实很简单,这是因为以前你安装过Apache,Apache监听的也是80端口,所以会冲突。

把httpd进程给杀了就行,也能去改端口号,但是太麻烦了。不过刚才我杀进程的时候竟然杀不死。。。

干脆把httpd的开机自启关了
chkconfig httpd off

然后重启就可以解决了。

作者:石乐志的LK
链接:https://www.jianshu.com/p/574d848587ab
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值