1、安装依赖、关闭防火墙
[root@localhost ~]# yum install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
2、创建nginx启动用户
注意:会在/home目录生成对应www的用户目录
[root@localhost ~]# /usr/sbin/groupadd -f www ---建立用户组 www
[root@localhost ~]# /usr/sbin/useradd -g www www ---添加用户 www
创建文件存放目录
[root@localhost ~]# mkdir /pages
3、下载nginx
[root@localhost ~]# cd /usr/local/src
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
4、解压并编译安装
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz
[root@localhost ~]# cd nginx-1.14.0
[root@localhost ~]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
[root@localhost ~]# make
[root@localhost ~]# make install
5、设置nginx软连接
[root@localhost ~]# ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/
6、配置nginx服务并设置开机自启
设置service启动的方式
[root@localhost ~]# vim /etc/init.d/nginx
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
if [ -s /bin/ss ]; then
StatBin=/bin/ss
else
StatBin=/bin/netstat
fi
case "$1" in
start)
echo -n "Starting $NAME... "
if $StatBin -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if $StatBin -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is is stopped."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if $StatBin -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
[root@localhost ~]# chmod +x /etc/init.d/nginx
设置开机自启动
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
7、启动nginx
[root@localhost ~]# /etc/init.d/nginx start 启动
[root@localhost ~]# service nginx start 启动
[root@localhost ~]# service nginx stop 停止
[root@localhost ~]# service nginx restart 重启
[root@localhost ~]# service nginx status 查看nginx状态
8、访问
访问http://IP即可看到nginx欢迎页,默认欢迎页面在/usr/local/nginx/html目录下
9、修改nginx.conf配置文件
将下面的全部复制到nginx.conf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index
charset utf-8;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /pages {
alias /pages;
autoindex on; # 开启索引功能,以便可以看到文件列表
autoindex_exact_size off; # 显示文件大小
autoindex_localtime on; # 显示文件时间
}
}
}
修改完配置后,保存退出,重启Nginx:service nginx restart
11、创建文件测下
[root@localhost ~]# cd /pages/
[root@localhost ~]# touch 1.txt
访问http://IP/pages