Linux CentOS7 nginx源码包安装(正在使用)

8 篇文章 0 订阅

一、准备nginx运行环境(安装gcc环境、perl库、zlib、openssl等,每个安装都可能需要稍微等待一下下载)

gcc  nginx编译依赖gcc环境

yum install -y gcc-c++

这里执行上述命令的时候我的服务器报错:Error downloading packages ....  [Errno 5] [Errno 12] Cannot allocate memory 。解决方式看这里 Linux 安装gcc或者其他应用时报错: Error downloading packages ....  [Errno 5] [Errno 12] Cannot allocate memory_test-CSDN博客

perl (Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式

yum install -y pcre pcre-devel

#如果上面yum方式安装失败,也可以尝试手动安装,参考地址:

Linux 安装 nginx 安装PCRE库(1)_大洋-CSDN博客_pcre安装

linux安装pcre - 简书

zlib 该库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip

yum install -y zlib zlib-devel

openssl 一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http)

yum install -y openssl openssl-devel

以上四个执行命令结束后都会提示 Complete!表示安装成功:

 

有的系统可能安装完上述环境需要重启服务器才能生效,如需重启的话,使用命令(我装的时候跳过了这步,没有执行重启):

reboot

二、下载安装nginx 

下载nginx,nginx: download ,下载页面从上到下依次是开发版,稳定版,往期历史版本,这里下载稳定版 nginx-1.16.0 

上传下载的nginx-1.16.0.tar.gz文件到linux服务器的 /usr/local/ 目录下,并解压文件到当前目录,进入nginx-1.16.0

cd /usr/local/

tar -zxf nginx-1.16.0.tar.gz

cd nginx-1.16.0

检查安装环境是否有问题,有问题的话会报错,下面是一行命令  不是两行!!

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

检查后有个error,这不是检查出来的错误,是目录和文件名,不用管它,整体检查结果是没问题的,好,继续。

编译

make

安装

make install

安装完成后,并没有其他报错信息。返回上层目录,查看多了一个nginx目录,表示安装成功

三、运行测试nginx

尝试运行nginx(图为不同位置启用nginx方式,没有报错信息,表示命令执行成功):

/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop

cd /usr/local/nginx/sbin/
./nginx
./nginx -s reload
./nginx -s stop

./nginx

启动nginx之后,可以查看80端口占用情况(nginx默认占用80端口),确认nginx是否已运行,并且使用了80端口:

netstat -lnp|grep 80

或者直接查看nginx相关服务是否开启

netstat -lnp|grep nginx 

浏览器访问服务器外网ip地址,看是否展示nginx默认页面:http://22.22.3233.21:80/

如果nginx运行了,并且占用的是80端口,但是ip地址访问不到nginx默认的页面,有可能是阿里云安全组或者防火墙没有开通80端口对外访问:

阿里云安全组(阿里默认开通80端口):

防火墙

查看防火墙的端口开通情况

firewall-cmd --zone=public --list-ports

或者

firewall-cmd --list-ports

如果80端口不存在,则添加,并重载防火墙

firewall-cmd --permanent --zone=public --add-port=80/tcp

firewall-cmd --reload 

加入后重新浏览器访问ip地址,如果还是访问不到nginx,那就自行排查吧。

服务器未备案,也可能导致80端口访问不通,优先尝试备案,备案后访问80端口即可;或者考虑修改nginx.conf的 server配置 :

listen       81;# 将原端口80 修改为81

然后重启nginx,浏览器访问81端口 http://22.22.3233.21:81/

四、开机自启

1. 编辑rc.local文件,添加启动nginx的命令

vi /etc/rc.local

文件末尾新增启动nginx的命令,保存并退出

/usr/local/nginx/sbin/nginx

2. 修改 /etc/rc.local 的权限

chmod 755 /etc/rc.local

3. 系统重启后,访问ip或ip:80,默认跳转到nginx首页,则表示开机自启生效

五、nginx.conf配置

nginx.conf 配置文件位置  /usr/local/nginx/conf/

可以下载该文件到本地编辑 然后上传覆盖原文件 并重启nginx   ,也可以直接执行vi命令行编辑文本内容 保存并重启nginx

#user  nobody;
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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        # 原80端口改为使用81
        listen       81;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        # ================== 自定义配置 ========================
        #微服务后台 前端VUE 域名根目录访问项目
        #location /{
        #    add_header Access-Control-Allow-Origin '*' always; # 解决跨域访问问题
        #    alias   /home/ruoyi/dist;#项目前端文件所在目录
        #    try_files $uri $uri/ /index.html;#自动寻找路径 找不到则默认访问index.html
        #    index  index.html index.htm;
        #}
        #微服务后台 前端VUE
        location /admin {
            add_header Access-Control-Allow-Origin '*' always; # 解决跨域访问问题
            alias   /home/ruoyi/dist;#项目前端文件所在目录
            try_files $uri $uri/ /index.html;#自动寻找路径 找不到则默认访问index.html
            index  index.html index.htm;
        }
        #微服务 通过前端访问接口
        location /prod-api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/;
        }
        #微服务后台 后端接口
        location /api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/;
        }
        # ==========================================
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

 

---------------------

六、清除不必要文件

至此 /usr/localhost/目录下的 nginx-1.16.0 和 nginx-1.16.0.tar.gz 都可以删掉了。

rm -rf nginx-1.16.0

rm -rf nginx-1.16.0.tar.gz

参考:

3. CentOS 7.6 Nginx的安装_zhangningkid的博客-CSDN博客

CentOS 7 安装 Nginx_Star's Tech Blog-CSDN博客

Centos7下安装nginx并且设置nginx开机启动_仲翎逸仙-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值