nginx编译安装教程

环境:

  • CentOS 8
  • nginx-1.19.10

安装依赖项

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

下载安装包

下载链接:
nginx: download

安装

上传到指定目录,创建nginx目录

mkdir /www/nginx
cd /www/nginx
tar -zxvf  nginx-1.19.10.tar.gz

修改文件夹的权限为root用户,为了有执行权限
chown -R root:root nginx-1.19.10

创建用户nginx:

groupadd -f www  # 创建组
useradd -g www www # 创建组以及设置用户
usermod -s /sbin/nologin nginx #修改用户默认shell /sbin/nologin

配置nginx选项

./configure --user=nginx --group=nginx --prefix=/www/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module
--user:指定运行nginx服务的用户
--group:指定运行nginx服务的组
--prefix:指定安装nginx的主目录
--with:启用模块

编译nginx

make && make install

查看目录结构

cd /www/nginx
ls
conf :配置文件目录
html:网页文件目录
logs:日志和pid进程文件
sbin:二进制启动文件
cd sbin     # 进入目录
./nginx -V  #查看编译参数
./nginx -V  #查看nginx版本

nginx进程管理

./nginx 回车进程启动
netstat -nltp 查看使用的端口
ps -aux | grep nginx 查看进程
kill 进程号,上面netstat -nltp前面的号码就是进程号

尝试访问nginx,如果你的服务器80端口是开启的,否则开启防火墙端口

http://10.10.0.212

使用systemctl管理进程

vim /usr/lib/systemd/system/nginx.service文件,相关目录替换成自己的目录。
文件示例:

/usr/lib/systemd/system 新建nginx.service文件

[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
Wants=network.target

[Service]
PIDFile=/www/nginx/logs/nginx.pid
WorkingDirectory=/www/nginx
ExecStart=/www/nginx/sbin/nginx
Restart=on-abnormal
RestartSec=5s
KillMode=mixed

StandardOutput=null
StandardError=syslog

[Install]
WantedBy=multi-user.target

重启服务
systemctl daemon-reload # 刷新缓存
systemctl start nginx # 启动进程
systemctl enable nginx # 开机启动进程

最后附上一个比较完整的nginx.conf配置文件模板

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

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

http
    {
        include       mime.types;
                #include luawaf.conf;

                include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
                fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
                limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 888;
        server_name phpmyadmin;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;
            location ~ /tmp/ {
                return 403;
            }

        #error_page   404   /404.html;
        include enable-php-74.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
include /www/server/panel/vhost/nginx/*.conf;
}

posted @ 2021-08-09 15:03  骑着蜗牛路过你的风景  阅读(179)  评论(0编辑  收藏  举报