跳至主要內容

linux安装nginx

tanmantang原创大约 3 分钟开发工具linuxnginx

Linux 安装 nginx

下载Nginx安装包

进入Nginx官网下载页 https://nginx.org/en/download.htmlopen in new window,选择Stable version下的第二个链接 nginx-1.24.0

或点此直接下载open in new window

对于服务器有外网权限的可直接使用wget工具下载

wget https://nginx.org/download/nginx-1.24.0.tar.gz

安装依赖包

yum install -y gcc gcc-c++ 
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl*

上传并解压

这里我上传到/data/nginx/目录里面

解压

cd /data/nginx/
tar -zxf nginx-1.24.0.tar.gz

编译安装

预编译

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
  1. --prefix是指定安装目录(建议指定)不指定的话,默认/usr/local/nginx
  2. --with-http_stub_status_module 监视模块,可以查看目前的连接数等一些信息
  3. --with-http_ssl_module ssl模块
  4. –-with-http_gzip_static_module 扩展压缩模块

执行后若报错如下

checking for OS
 + Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

请安装gcc、gcc-c++

yum install -y gcc gcc-c++ 

若报错如下

make: *** No rule to make target `build', needed by `default'. Stop.

请安装openssl

yum install -y openssl*

编译并安装

make && make install 

修改配置文件

cd /usr/local/nginx/conf
# 修改默认配置文件,不用备份,官方自己就有一份原始备份,nginx.conf.default
vim nginx.conf

为了方便维护,我们采取通过include的方式导入其他配置文件,而不去改动主nginx配置文件

删除 http 里面所有server,然后添加导入配置文件路径,修改为如下

#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 {
    includemime.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;

	sendfileon;
    #tcp_nopush     on;

	#keepalive_timeout  0;
    keepalive_timeout  65;

	#gzip  on;
    
    ## 该路径你可以随意指定,之后你只需要将配置文件放入该路径即可
    include /data/nginx/conf/*.conf;
}

新建配置文件

因为刚刚已经指定了/data/nginx/conf/目录下面的所有conf文件,所以现在到conf目录新建配置文件测试

cd /data/nginx/conf/
vim nginx.conf

保存以下内容,当然你也可以复制 之前删掉的那些内容 来作为你的配置文件

一个简单的配置示例

server {
  listen 8080; # 监听8080端口
  server_name  localhost;
    
  location / {
    root   /your/dist/html; # 该路径指向你的前端页面完整路径
    index  index.html index.htm;
  }

  # 配置404页面
  error_page 404 /404.html;
    
  # 配置50* 页面
  error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
  }
}

之前删除的内容{#beforedel}

点击打开查看详情
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

cd /usr/local/nginx/sbin
./nginx

常用命令

你需要进入你的安装目录下面的sbin目录,如本文是 /usr/local/nginx/sbin

强制停止命令nginx -s stop
优雅停止命令(等最后一次交互执行完再停止)nginx -s quit
检查配置文件是否出错nginx -t
检查指定配置文件是否出错nginx -t /data/nginx/conf/nginx.conf
重新加载命令nginx -s reload
查看Nginx版本nginx -v
查看Nginx详细信息nginx -V

相关链接:

nginx重新编译添加ssl模块--with-http_ssl_moduleopen in new window