Linux下安装Nginx,并使用Nginx做静态资源服务器

一.安装依赖库
          yum install gcc-c++
          yum install pcre pcre-devel
          yum install zlib zlib-devel
          yum install openssl openssl-devel

二.安装Nginx

下载地址

进入http://nginx.org/en/download.html  下载想要的版本

1. 把nginx的源码上传到linux系统

放在/usr/local/下

2. 把压缩包解压缩

tar -zxvf nginx-xxxxx.tar.gz

3运行代码安装nginx

./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/code/pcre-8.32 \
--with-zlib=/usr/local/code/zlib-1.2.11

make

扫描二维码关注公众号,回复: 5083391 查看本文章

make -install

注意:  --with-pcre=/usr/local/code/pcre-8.32 \
          --with-zlib=/usr/local/code/zlib-1.2.11

为源码路径  ,需要去官网下载源码上传进linux解压手动指定,否则make时会报错:

make[1]: *** [/usr/local/pcre//Makefile] Error 127

四.安装好nginx后修改nginx的配置文件

cd /usr/local/nginx

vi nginx.conf

location ^~ /static/ {
            root /usr/local/;
        }

修改如图,listen为监听端口,默认为80,这里改为81,防止起冲突

^~ 表示匹配任意前缀字符 static表示只要请求路径中包含static就会访问到此规则下。 root 是指路径,需在/usr/local/下创建static文件夹

cd /usr/local

mkdir static

丢两张图片到 /usr/local/static 下

五.启动nginx

/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf
nginx路径 -c 配置文件路径

六.效果

1.直接访问nginx

2.访问静态资源

 

猜你喜欢

转载自blog.csdn.net/qq_34907455/article/details/82501345