首先要确保本地yum配置
1.安装支持软件
yum -y install pcre-devel zlib-devel openssl-devel
2.创建运行用户和组
useradd -M -s /sbin/nologin nginx
3.编译安装nginx
rz(nginx-1.14.2.tar.gz)
4.解压源码包
tar xvf nginx-1.14.2.tar.gz -C /usr/src/
5.配置编译
cd /usr/src/nginx-1.14.2/
6.编译安装
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
如果编译安装失败则是要进行安装gcc、gcc-c++:yum -y install gcc gcc-c++。
7.创建链接文件
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
8.查看权限
ll /usr/local/bin/nginx
9.起服务
nginx
10.查看端口
netstat -anpt |grep :80
11.编写nginx服务脚本
vim /etc/init.d/nginx
添加内容:
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
12.给该文件添加执行权限
chmod +x /etc/init.d/nginx
13.通过脚本可以启动、停止、重启、重载服务器
chkconfig --add nginx
chkconfig nginx on
chkconfig --list nginx
14.配置nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
添加内容:
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
worker_rlimit_nofile 102400;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 4096;
}
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
15.修改nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf 添加内容:(第一个location下)
location /status {
stub_status on;
access_log off;
}
16.重启服务
killall -HUP nginx