nginx源安装和编译安装

nginx源安装

首先配置yum源
vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum install -y nginx
nginx -v
nginx version: nginx/1.26.0
 systemctl enable --now nginx

《nginx源安装和编译安装》

配置文件

[root@rocky1 ~]# rpm -ql nginx

/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/.build-id
/usr/lib/.build-id/9d
/usr/lib/.build-id/9d/714b7d3e8ee95ae44331fa0a6427d14e264612
/usr/lib/.build-id/c8
/usr/lib/.build-id/c8/b1d7eaf447c99eb8bfa543c969b794a19a089a
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.26.0
/usr/share/doc/nginx-1.26.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

主配置文件
cat /etc/nginx/nginx.conf

user  nginx;  可以修改用户和组 user nobody nogroup;
worker_processes  auto; auto是cpu的核数

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024; 一个worker进程支持的连接数,机器配置好改大
}


http {
    include       /etc/nginx/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;

    include /etc/nginx/conf.d/*.conf;
}
groupadd www
useradd -g www -s /sbin/nologin www
id www
uid=1000(www) gid=1000(www) groups=1000(www)
mkdir -p /data/www
sudo chown -R www:www /data/www
echo "test nginx" > /data/www/test/index.html

添加配置文件

server {
    listen       80;
    listen       801;监听多个端口,可以只监听一个
    server_name testnginx.org
    test.testnginx.org
    www.testnginx.org;配置多个域名,可以只配置一个


    location / {
        root   /data/www/test;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

检查nginx的配置文件是否正确

nginx -t

重载nginx的配置

nginx -s reload

或者

systemctl reload nginx 

《nginx源安装和编译安装》

《nginx源安装和编译安装》

《nginx源安装和编译安装》

《nginx源安装和编译安装》

编译安装nginx

#安装nginx必备依赖库

yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget https://nginx.org/download/nginx-1.26.0.tar.gz
tar zxf nginx-1.26.0.tar.gz
cd nginx-1.26.0

创建一个系统用户,指定shell

useradd -r -s /sbin/nologin nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx  --with-http_ssl_module
make -j 8 && make install

配置环境变量

echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
source /etc/profile

配置服务

cat > /etc/systemd/system/nginx.service  << EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
EOF
[root@centos70 nginx-1.26.0]# systemctl enable --now nginx
[root@centos70 nginx-1.26.0]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-04-29 09:33:17 UTC; 1s ago
  Process: 6075 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 6076 (nginx)

《nginx源安装和编译安装》

nginx安装完成后如何添加新的正向代理模块

该模块最新只支持1.25的nginx,重新安装nginx1.24

wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxf  nginx-1.24.0.tar.gz
wget https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v0.0.6.tar.gz
tar -zxf v0.0.6.tar.gz
rm -f v0.0.6.tar.gz
cd nginx-1.24.0/
mv /usr/local/nginx /usr/local/nginx1.26
yum -y install patch 
patch -p1 < /root/software/ngx_http_proxy_connect_module-0.0.6/patch/proxy_connect_rewrite_102101.patch
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx  --with-http_ssl_module --with-http_stub_status_module  --with-http_realip_module  --with-threads --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --add-module=/root/software/ngx_http_proxy_connect_module-0.0.6

make -j 8 && make install

tomcat动静分离

用81端口和tomcat.rocky.com来访问tomcat的动态文件
html和图片等静态资源文件交给nginx处理

server {
        listen 81;
        server_name tomcat.rocky.com;
        location / {
                proxy_pass http://127.0.0.1:8080;
        }

        location ~ .*\.(htm|html|jpg|jpeg|png|bmp){
                root /data/www/nginx_tomcat/webapps;
                #资源在浏览器缓存时间为1天
                expires 1d;
        }
}
echo "nginx tomcat webapps"> /data/www/nginx_tomcat/webapps/index.html
[root@eab5aec2df6c webapps]# cat > /usr/share/tomcat/webapps/ROOT/index.jsp <<EOF
>
> <%
>     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
>     String formattedDate = sdf.format(new java.util.Date());
> %>
> <%= formattedDate %>
> EOF

《nginx源安装和编译安装》

nginx连接php

php-fpm的配置有两种,分别是sock通信和端口通信
配置文件一般在/etc/php-fpm.d/www.conf下

listen = 127.0.0.1:9000
listen = /app/php74/var/run/php-fpm.sock
[root@eab5aec2df6c webapps]# cat > /data/www/test/php/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF

《nginx源安装和编译安装》
权限问题导致的
修改php-fpm的用户为nginx同nginx的用户一致
/etc/php-fpm.d/www.conf

user = nginx
group = nginx

listen.owner = nginx
listen.group = nginx

《nginx源安装和编译安装》
错误日志
FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream

路径及cgi配置错误,添加root为项目路径,同网站路径一致
修改

#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
改为
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

nginx配置

server {
    listen        80;
    listen       801;
    server_name testnginx.org
    test.testnginx.org
    www.testnginx.org;
    location / {
        root   /data/www/test;
        index  index.html index.htm;
    }
    location ~ \.php$ {
        root /data/www/test;
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass   unix:/app/php74/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;

    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

或者这样更方便root统一放在server下,改为端口监听

server {
    listen        80;
    listen       801;
    server_name testnginx.org
    test.testnginx.org
    www.testnginx.org;
    root   /data/www/test;
    location / {

        index  index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass   unix:/app/php74/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;

    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

/etc/php-fpm.d/www.conf下修改php-fpm为端口
分号为注释,不是井号,注意一下

listen = 127.0.0.1:9000
;listen = /app/php74/var/run/php-fpm.sock

所有配置修改不要忘记重启

systemctl restart nginx
systemctl restart php-fpm

《nginx源安装和编译安装》

需要默认php为index页面,可以如下配置

index  index.php index.html index.htm;

《nginx源安装和编译安装》

参考

Nginx安装和配置(yum安装和编译安装)

点赞

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注