1.安装编译软件及nginx依赖
#### yum安装依赖
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
##### gcc编译软件
##### nginx的http模块解析正则表达式需要 pcre
##### zlib 资源压缩
##### openssl nginx开启ssl所需要
2.下载nginx1.14.0源码包及安装
#### 进入root目录
cd /root
#### 下载
wget http://nginx.org/download/nginx-1.14.0.tar.gz
#### 解压
tar -zxvf nginx-1.14.0.tar.gz
#### 进入nginx目录
cd nginx-1.14.0
#### 进行配置
./configure --prefix=/usr/local/lnmp/nginx \
--sbin-path=/usr/local/lnmp/nginx/sbin/nginx \
--conf-path=/usr/local/lnmp/nginx/conf/nginx.conf \
--pid-path=/usr/local/lnmp/nginx/logs/nginx.pid \
--error-log-path=/usr/local/lnmp/nginx/logs/error.log \
--http-log-path=/usr/local/lnmp/nginx/logs/access.log \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
#### --with-http_stub_status_module 监控nginx状态
#### --with-http_ssl_module 支持ssl
#### --with-http_gzip_static_module 静态压缩
#### 进行编译
make
#### 进行安装
make install
nginx官方编译参数解释
3.启动nginx
#### 创建www用户组和www用户
groupadd www
useradd -g www -s /sbin/nologin -M www
#### -M 不创建家目录 建议与php使用同一用户组及用户
#### 彻底删除用户
userdel -r name
#### 检查nginx配置
/usr/local/lnmp/nginx/sbin/nginx -t
#### 启动nginx
/usr/local/lnmp/nginx/sbin/nginx
#### 查看nginx运行情况
ps aux | grep nginx
4.编辑nginx开机脚本
#! /bin/bash
#chkconfig: 2345 80 90
#description:nginx run
DESC="nginx daemon"
NAME=nginx
DAEMON="/usr/local/lnmp/nginx/sbin/nginx"
CONFIGFILE="/usr/local/lnmp/nginx/conf/nginx.conf"
PIDFILE="/usr/local/lnmp/nginx/logs/nginx.pid"
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start()
{
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop()
{
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload()
{
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
官方脚本查看
5.设置nginx开机自启
#### 编写脚本 复制上面脚本代码 保存
vim /etc/init.d/nginx
#### 修改服务脚本的执行权限
chmod 755 /etc/init.d/nginx
#### nginx加入服务
chkconfig --add nginx
#### nginx 设置为开机启动
chkconfig nginx on
#### 现在就可以用 service nginx status 等命令了
6.http配置参考
user www www;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].";
server_names_hash_bucket_size 128;
client_max_body_size 100m;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
include /usr/local/lnmp/nginx/conf.d/*.conf;
}
7.站点配置参考
server {
listen 80;
server_name 127.0.0.1;
root html;
location / {
index index.html index.htm index.php l.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}