版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/q85795362/article/details/82891997
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
# nginx This shell script takes care of starting and stopping
# nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/usr/local/lnmp/nginx/sbin/nginx"
NGINX_CONF="/usr/local/lnmp/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/lnmp/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
if [ -s ${NGINX_PID} ]; then
echo "nginx already running"
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF}
if_no start $?
else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac
官方脚本查看
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 {
}
}