欢迎光临
我们一直在努力

Linux7-nginx运行环境编译安装及配置

  • 安装环境(centos7)

centos 3.10.0-957.1.3.el7.x86_64

  • 安装nginx所需要的依赖库
yum -y install gcc-c++ zlib-devel openssl-devel pcre-devel
  • 下载nginx源码并解压然后进入解压目录.(源码去官网下载, 以下是官方链接)
    wget -c http://nginx.org/download/nginx-1.15.8.tar.gz
    tar -zxvf nginx-1.15.8.tar.gz
    cd nginx-1.15.8
  • 建立nginx用户及用户组
    groupadd -r nginx
    useradd  -s /sbin/nologin -g nginx -r nginx
  • 配置安装环境
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module
#注:这里--with开头的选项为nginx自带的模块,需要什么就添加,默认是不安装,
  • 编译安装
make && make install
  • 启动nginx
/usr/local/nginx/sbin/nginx
  • 修改nginx.conf配置文件
vi /usr/local/nginx/conf/nginx.conf

#user  nobody; #把前面的#号去掉,把nobody修改为上面建立的用户名:nginx
worker_processes  1;#cpu几个线程,一般修改为几。

#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;
    #开启nginx的gzip压缩
    #gzip配置开始
    gzip on;
    gzip_min_length  1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 3;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on; 
    #gzip配置结束

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
    #    }
    #}

}
  • 重新加载nginx.conf配置文件
/usr/local/nginx/sbin/nginx -s reload
  • nginx添加开机启动
chmod +x /etc/rc.d/rc.local  #给予执行权限
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local #设置开机启动
  • 检查nginx启动是否正常,我这里CPU是双线程,所以有二个工作线程
[root@localhost ~]# ps ax | grep nginx
 3396 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
13657 ?        S      0:10 nginx: worker process
13658 ?        S      0:11 nginx: worker process
16189 pts/0    S+     0:00 grep --color=auto nginx

启动后,我们输入服务器的公网IP,就可以正常打开nginx的默认欢迎页面,若是启动正常了,就是网站打不开,那就看下防火墙( firewalld )是否开启了80端口或是http服务

赞(0) 打赏
原创文章转载请注明出处:爱编程 » Linux7-nginx运行环境编译安装及配置
分享到: 更多

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

爱编程、一个运维兼程序员的博客!

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏