Centos7编译安装Nginx

一、安装工具及依赖

yum -y install gcc gcc-c++ autoconf automake make 
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

这里很多依赖之前装python3时候装过,再试一遍也不会重复安装

二、下载及解压nginx

wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar –zxvf nginx-1.12.2.tar.gz

进入解压后的文件夹

cd nginx-1.12.2/

建立安装目录

vi /usr/local/nginx

指定安装目录

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream


#./configure --prefix=/usr/local/nginx #带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了 #http_stub_status_module 状态监控 #http_ssl_module 配置https #stream 配置tcp得转发 #http_gzip_static_module 压缩 #http_sub_module 替换请求

正常无错误后

#编译安装
make && make install

安装成功后,会在./configure --prefix=/usr/local/nginx,指定的目录/usr/local/nginx创建4个 文件夹

#启动 nginx服务
/usr/local/nginx/sbin/nginx
#停止服务
/usr/local/nginx/sbin/nginx -s stop

#重启服务
/usr/local/nginx/sbin/nginx -s reload 

#查看启动情况
ps -ef|grep nginx

#查看端口情况
netstat -ano|grep 80

#查看nginx版本
./sbin/nginx -V

三、配置防火墙

[root@localhost nginx-1.12.2]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success

[root@localhost nginx-1.12.2]# firewall-cmd --reload
success

四、配置nginx

打开配置文件

[root@localhost nginx-1.12.2]# vi /usr/local/nginx/conf/nginx.conf

下面配置供参考

#user  nginx;
worker_processes  1;

#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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    gzip on; # 开启Gzip
    


    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /opt/dist;
            index  index.html;
        }

    }
    server {
        listen       8000;
        server_name  localhost;

        location / {
            uwsgi_pass localhost:9090;
            include /usr/local/nginx/conf/uwsgi_params;
        }
        

    }

}

创建nginx启动命令脚本

vi /etc/init.d/nginx

脚本内容

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.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
chmod a+x /etc/init.d/nginx #设置执行权限
chkconfig --add nginx  #注册成服务
chkconfig nginx on  #设置开机启动

 

或者

systemctl enable nginx

启动nginx

#启动nginx服务
service nginx start
#停止nginx服务
service nginx stop
#重启nginx服务
service nginx restart
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
service nginx reload

大功告成,nginx具体使用得单独深入学习,这里仅仅说明如何安装并且配置使用

posted @ 2019-11-22 09:32  丹尼伯夷  阅读(280)  评论(0编辑  收藏  举报