centos8安装nginx

centos8安装nginx

  1. nginx说明
    Nginx是一种轻量级,高性能,多进程的Web服务器,其特点就是占用内存少并发能力强,因此对于需要高并发的网站而言是最好的选择。它HTTP 服务器有以下几项基本特性:
    (1) 处理静态文件,索引文件以及自动索引,打开文件描述符缓冲。
    (2)无缓存的反向代理加速,简单的负载均衡和容错
    (3)模块化的结构,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,如果由FastCGI或其它代理服务器处理蛋液中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。
    (4)支持SSL和TLSSNI。
    Nginx下载地址.
  2. Nginx准备工作
    Nginx的安装依赖于三个包(这三个是必须的):SSL功能需要openssl库,gzip模块需要zlib库,rewrite模块需要pcre库,
    安装这三个包有两个办法:
    (1)独自下载,源码安装:
    openssl库下载地址:http://www.openssl.org/
    zlib库下载地址:http://www.zlib.net/
    pcre库下载地址:http://www.pcre.org/
    下载之后独自解压,进入安装目录 配置(./config),编译(make) ,安装(make install)
    (2)通过yum直接安装
    如下:
//安装包式安装
tar -zxvf  soft/openssl-3.0.0-alpha8.tar.gz
cd  openssl-3.0.0-alpha8/
./config
make && make install 
//yum安装(上下两种选其中之一就行)
  yum -y install lrzsz   gcc-c++  pcre pcre-devel  zlib zlib-devel ruby openssl openssl-devel patch wget bash-completion zlib.i686 libstdc++.i686 lsof unzip zip
 // 最少的安装依赖
  yum -y install pcre-devel openssl-devel gcc lrzsz openssh-clients
  1. 下载nginx
    Nginx下载.
    注意,本人将nginx下载在usr/local/src下
  //创建用户组
  groupadd -r www
  useradd -r -g www  www
  //进入安装目录
  cd /usr/local/src
  //下载nginx
  wget http://nginx.org/download/nginx-1.19.5.tar.gz
  //解压文件
  tar -zxvf nginx-1.19.5.tar.gz
  1. 安装nignx
  //进入解压后的文件
 cd nginx-1.19.5
  //编译安装:设置基本参数
  //prefix nginx 文件的安装路径
  //sbin-path:nginx 二进制程序的路径名 一般情况下是默认,启动文件
  //conf-path:设置 nginx.conf 的路径
  //user 权限 group组
./configure \
--prefix=/usr/local/nginx \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--user=www\
--group=www \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
//编译 安装 这个过程持续的时间很长
make && make install

修改配置文件:

vi  /etc/nginx/nginx.conf 
#user nobody
改为 user www www

创建文件:

 mkdir -p /var/tmp/nginx/client

安装完成之后就能在/usr/local/下看到nginx目录
7. 打开防火墙
首先在阿里云上开放端口(80)
再在本机服务器上开放端口(80)

//添加端口
  firewall-cmd --zone=public --add-port=80/tcp --permanent
  //重启防火墙
  systemctl restart firewalld
  1. 启动nginx
//nginx在启动文件位置
sudo /usr/sbin/nginx

这时就可以在网站上看到结果了
在这里插入图片描述
或者是在服务器中输入

curl http://localhost

10.其他操作:
添加启动脚本(为了更方便管理使用nginx):

vim /etc/init.d/nginx.sh

注意:NGINX_CONF_FILE 为配置文件位置
nginx 为启动文件位置

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
killall -9 nginx
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
    $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

添加脚本执行权限

chmod +x /etc/init.d/nginx.sh

添加nginx服务为系统服务(添加成功后就可以执行service nginx start、stop、restart、status等操作了)

chkconfig --add nginx.sh

设置终端模式开机启动

chkconfig nginx.sh on

启动nginx

service nginx start 

停止nginx

service nginx stop

重启nginx

service nginx restart

11.安装中的问题:

nginx: [emerg] getpwnam(“www”) failed

解决方案是

vi  /etc/nginx/nginx.conf 
#user nobody
改为 user www www

修改防火墙时

WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release.
//方案是:
vim /etc/firewalld/firewalld.conf
AllowZoneDrifiting = yes 改为no
//重启防火墙
systemctl restart firewalld

11.文件转载:
https://blog.csdn.net/weixin_43247563/article/details/105505197
https://blog.csdn.net/qevery678/article/details/105055381

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值