centos8.2编译安装nginx1.18

查看服务器版本号

cat /etc/redhat-release
#CentOS Linux release 8.2.2004 (Core)

安装依赖

#更新源
yum update -y
yum install -y gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

初始化

#初始化用户
adduser www
groupadd www
usermod -G www www

#初始化目录 
mkdir -p /data/app/nginx
mkdir -p /data/www
mkdir -p /data/download
mkdir -p /data/logs
chown www.www /data/logs
mkdir -p /data/logs/www
chown www.www /data/logs/www
mkdir -p /data/logs/nginx
chown www.www /data/logs/nginx

编译安装nginx

#下载Nginx  
cd /data/download/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -zxvf nginx-1.18.0.tar.gz 
cd nginx-1.18.0
#编译安装 
./configure --prefix=/data/app/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-pcre --with-pcre-jit

#--prefix=/usr/local/nginx-1.12.0             # 定义安装路径,不写时默认为/usr/local/nginx
#--user=nginx                                 # 在配置文件中没有指定user指定时,worker进程的运行身份,不写时默认为nobody
#--group=nginx                                # 在配置文件中没有指定user(不是group,配置文件中没有group指令)指定时,worker进程的运行组
#--with-http_ssl_module        # 启用ssl功能
#--with-pcre                   # 设置pcre库的路径,yum安装的pcre-devel可以不写路径

make && make install

# 查看nginx的版本
/data/app/nginx/sbin/nginx -v

配置多个站点

cp /data/app/nginx/conf/nginx.conf /data/app/nginx/conf/nginx.conf.bak
cat > /data/app/nginx/conf/nginx.conf << EOF
user  www www;
worker_processes 1;

error_log  /data/logs/nginx/nginx_error.log  warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections 10240;
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    charset utf8;

    server_tokens off;
    server_name_in_redirect off;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 16k;

    client_max_body_size 50m;
    client_body_buffer_size 128K;

    sendfile  on;
    tcp_nopush on;
    keepalive_timeout 60;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 32 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 156k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

    log_format snlog '\$remote_addr | \$remote_user | \$time_local | \$request | \$status | '
            '\$bytes_sent | \$body_bytes_sent | \$host | \$http_referer | \$http_user_agent | '
            '\$upstream_addr | \$gzip_ratio | \$http_x_forwarded_for | \$request_time | \$upstream_response_time';

    access_log off;

    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    proxy_connect_timeout      300;
    proxy_send_timeout         300;
    proxy_read_timeout         300;

    proxy_buffering on;
    proxy_buffer_size          32k;
    proxy_buffers              128 32k;
    proxy_busy_buffers_size    256k;
    proxy_temp_file_write_size 256k;

    upstream unix__tmp_php_cgi_sock {
        server unix:/tmp/php-cgi.sock weight=100 max_fails=5 fail_timeout=30;
        server unix:/tmp/php-cgi2.sock weight=100 max_fails=5 fail_timeout=30;
        server unix:/tmp/php-cgi3.sock weight=100 max_fails=5 fail_timeout=30;
    }


    # 禁止未授权访问
    server {
        listen 80 default_server;
        server_name _;

        location / {
            return 444;
        }
        access_log  off;
    }

    include vhost/*.conf;
}
EOF

# 配置多域名
mkdir -p /data/app/nginx/conf/vhost
cat > /data/app/nginx/conf/vhost/default.conf << EOF
server {
    listen  80;
    server_name www.host.com www.host1.com;
    index index.html index.htm index.php;
    root  /data/www/default;

    # 配置tp框架隐藏index
    location / {
        if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
        }
        index  index.html index.htm index.php;
    }

    location ~ /\. {
        deny  all;
    }
    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

    location ~ \.php$
    {
        index index.php;
        try_files $uri =404;
        fastcgi_pass    unix__tmp_php_cgi_sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME $fastcgi_script_name;
    }
    #access_log  /data/logs/www/host.com.log  snlog;
}

#server {
#    #443端口
#    listen 443 ssl;
#    #域名
#    server_name  ~^(?<subdomain>.+).host.com;
#    #证书和key目录
#    ssl_certificate      /data/www/default/ssl/host.com.pem;
#    ssl_certificate_key  /data/www/default/ssl/host.com.key;
#
#    index index.html index.htm index.php;
#    #指向的网页文件
#    root  /data/www/default;
#    # 配置tp框架隐藏index
#    location / {
#        if (!-e $request_filename) {
#            rewrite  ^(.*)$  /index.php?s=$1  last;
#            break;
#        }
#    }
#
#    location ~ \.php$
#    {
#        index index.php;
#        try_files $uri =404;
#        fastcgi_pass    unix__tmp_php_cgi_sock;
#        include         fastcgi_params;
#        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
#        fastcgi_param   SCRIPT_NAME $fastcgi_script_name;
#    }
#    #Log生成位置
#    #access_log  /data/logs/www/*.host.com.log  snlog;
#}

#http自动跳转https
# server {
    # listen  80;
    # server_name *.host.com;
    # rewrite ^(.*) https://*.host.com$1 permanent;
# }
EOF

启动配置


# 查看配置是否正确
/data/app/nginx/sbin/nginx -t

# 启动
/data/app/nginx/sbin/nginx
# 停止
/data/app/nginx/sbin/nginx -s stop
# 平滑重新启动
/data/app/nginx/sbin/nginx -s reload

## 开机自启 systemctl管理Nginx
#编写Nginx服务配置文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx	#描述服务
After=network.target	#描述服务类别

[Service]	#服务运行参数的设置
Type=forking	#是后台运行的形式
ExecStart=/data/app/nginx/sbin/nginx	#为启动命令
ExecReload=/data/app/nginx/sbin/nginx -s reload #为重启命令
ExecStop=/data/app/nginx/sbin/nginx -s stop	#为停止命令
PrivateTmp=true	#表示给服务分配独立的临时空间

[Install]#运行级别设置,可设置为多用户,即系统运行级别为3
WantedBy=multi-user.target


[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/data/app/nginx/sbin/nginx
ExecReload=/data/app/nginx/sbin/nginx -s reload
ExecStop=/data/app/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

#设置开机自启
systemctl start nginx.service
systemctl enable nginx.service

#nginx其他命令:
#注意: 如果当前的nginx服务不是systemd启动的.是查不到状态的
#ps -e | grep nginx 查询下nginx是否启动,如果启动请 /data/app/nginx/sbin/nginx -s stop 停止服务.
#然后再用systemd去管理nginx就OK了!
#systemctl start nginx (启动服务)
#systemctl stop nginx (停止服务)
#systemctl enable nginx (设置开机自启)
#systemctl disable nginx (停止开机自启)
#systemctl status nginx (查看状态)
#systemctl restart nginx (重启服务)

开机启动方法二


# 添加SysV启动脚本。
vim /etc/init.d/nginx
#以下为脚本写入内容
cat > /etc/init.d/nginx << EOF
#!/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="/data/app/nginx/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
EOF
 
# 赋予脚本执行权限。
chmod +x /etc/init.d/nginx
# 添加至服务管理列表,设置开机自启。
chkconfig --add nginx
chkconfig  nginx on
# 启动nginx
service nginx start
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页