美文网首页
LinuxCentOS7.4 安装配置nginx

LinuxCentOS7.4 安装配置nginx

作者: 55lover | 来源:发表于2017-11-21 12:01 被阅读0次

這里有2种方法(推荐第二种):

第一种:

1. 安装pcre pcre-devel

pcre 是 Perl库,解析nginx 中http模块的正则表达式 ,需要此库:

yum -y install pcre pcre-devel

2. 安装 zlib

zlib库 是提供多种解压和压缩的方式 (可选) 默认配置有gzip

yum -y install zlib zlib-devel

3. 安装 OpenSSL

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

yum -y install openssl openssl-devel

4. 开始安装nginx

通过wget 下载sourcecode :

官网地址: nginx下载官网 (目前是1.12.2是最稳定版本)

wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

tar zxvf nginx-1.12.2.tar.gz

接着 ,就使用它的默认配置,新手推荐

cd nginx-1.12.2

./configure

通过 make make install  进行编译安装

這里东西有点多,提示你不用担心,這是正常的

查看安装好nginx 目录 /usr/local/nginx (一般情况都在這里,我是windows系统,通过putty来实例linux系统,通过xftp查看在线文件夹(推荐))

可使用命令 whereis nginx 查看安装路径

接着 启动 nginx (注意 這里必须到 /usr/local/nginx/sbin/ 目录)

执行:

 ./nginx //启动

./nginx -s stop// 停止

./nginx -s quit// 从容停止 

./nginx- s reload// 重启 nginx

最后在浏览器中就可以输入你的ip去查看了(我是阿里云服务器 ,输入你公网ip即可)

追加配置i:开机启动和执行权限

开机启动:

回到跟目录 cd ~ 

vi /etc/rc.local 进入文件

摁下 i 键 进入编辑模式

在最后增加一行:/usr/local/nginx/sbin/nginx 

摁下 ESC 键 退出编辑模式

再输入 “ :wq! ” 强制退出文件 并保存

执行权限:

执行命令:chomd +x /etc/rc.d/rc.local

以上,是第一种方式,个人觉得比第二种麻烦

第二种:

第二种方式稍微简单点:直接通过yum安装nginx~

1. 添加nginx仓库

yum install epel-release -y

2. 下载Nginx

yum install nginx -y

3. 添加开机启动

systemctl enable nginx

4.修改Nginx配置文件

vi /etc/nginx/nginx.conf

摁下 i 键 进入编辑模式

添加配置内容:

server {

listen      80;

server_name jakexin.top,www.jakexin.top;            #绑定的域名

## 开启gzip

# gzip on;

##  启用gzip压缩的最小文件,小于设置值的文件将不会压缩

# gzip_min_length 1k;

## gzip 压缩级别,1-10,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明

# gzip_comp_level 2;

## 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。

# gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

## 是否在http header中添加Vary: Accept-Encoding,建议开启

# gzip_vary on;

## 禁用IE 6 gzip

# gzip_disable "MSIE [1-6]\.";

## 配置缓存

# location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {

# access_log  off;

# expires      30d;

# }

# location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {

# access_log  off;

# expires      24h;

# }

# location ~* ^.+\.(html|htm)$ {

# expires      1h;

# }

location /

{

proxy_set_header  X-Real-IP            $remote_addr;

proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_set_header  Host                  $http_host;

proxy_set_header  X-NginX-Proxy    true;

proxy_set_header  Connection "";

proxy_http_version 1.1;

proxy_pass http://127.0.0.1:3000;              #对应该的Nodejs程序端口

}

access_log  /mnt/log/www/jakexin_access.log;    #网站访问日志

}

摁下 ESC 键 退出编辑模式

再输入 “ :wq! ” 强制退出文件 并保存

注:#表示注释,這里的双#表示注释的是文本(用来解释该处语法意义)

5. 查看nginx 状态

nginx -t

在浏览器中输入您的ip 并查看,下图表示已经安装成功

6. 相关命令

service nginx restart

service nginx start

service nginx stop

...

完事儿~,好像简单很多!

关注一波!喜欢一波!本人是前端菜鸟,正在做自己的个人博客邓鹏的博客, 使用的技术 vue + koa2 + mysql + php + nginx!

相关文章

网友评论

      本文标题:LinuxCentOS7.4 安装配置nginx

      本文链接:https://www.haomeiwen.com/subject/rywivxtx.html