Centos6、7下安装Nginx的两种方法

2,623 阅读1分钟

Centos6、7下安装Nginx的两种方法

一,通过yum命令安装
1、操作系统版本的确认很重要,因为下一步我们要依靠这个来创建yum源文件
[代码](https://img2018.cnblogs.com/blog/940167/201811/940167-20181123141947437-996389736.png)

2、创建 nginx.repo
1
vi  /etc/yum.repos.d/nginx.repo
文件内容如下(如果是centos7,则对应的把数字6改成7):

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1


3、清除缓存并建立新的缓存
1
2
3
yum clean all
 
yum makecache
4、使用yum install nginx命令进行安装,输入y并回车


5、使用service nginx start命令启动nginx
1
service nginx start


6、查看Nginx进程是否存在


二、通过下载源码包进行安装
 https://blog.csdn.net/darkdragonking/article/details/79034654

 https://www.cnblogs.com/yaoximing/p/6068622.html

一、安装依赖
1. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

1
yum install gcc-c++
2. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

1
yum install -y pcre pcre-devel
3. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库

1
yum install -y zlib zlib-devel
4. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL1
yum install -y openssl openssl-devel
二、下载源码(去官网下载最新稳定版)
1
wget -c https://nginx.org/download/nginx-1.14.2.tar.gz


三、解压、编译
1
2
3
4
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2/
./configure
make && make install
四、启动以及停止
1
2
3
4
5
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
五、设置开机自启
即在rc.local增加启动代码就可以了

1
vim /etc/rc.local
增加一行

1
/usr/local/nginx/sbin/nginx 
设置执行权限:

1
chmod 755 rc.local


六、用systemctl管理
创建配置文件 
源码安装的nginx在/etc/systemd/system/multi-user.target.wants/目录下是没有nginx.service这个文件的,所以要新建

1
[root@localhost nginx-1.14.0]#vim /usr/lib/systemd/system/nginx.service
写入内容(全部复制进去,然后修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
 
[Install]
WantedBy=multi-user.target
修改 [Service]内容
1
2
3
4
将:
    ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf,
改为:
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
设置开机启动
1
[root@localhost nginx-1.14.0]# systemctl enable nginx.service
关闭之前启动的nginx服务
1
[root@localhost nginx-1.14.0]# pkill -9 nginx
重载修改过的所有配置文件
1
[root@localhost nginx-1.14.0]#systemctl daemon-reload
重新启动nginx服务
1
[root@localhost nginx-1.14.0]#systemctl start nginx

摘自网络:www.cnblogs.com/luxiaojun/p…,如有侵权,请联系删除