1.yum安装
yum安装法法简单,但是缺少灵活性;无法自定义模块及安装路径
配置yum源的配置文件:
在/etc/yum.repos.d 下编写一个nginx.repo
vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
然后安装:
make install nginx
2.源码安装
源码包获取:http://nginx.org/download/
源码安装法法过程繁琐,但是可定制化安装
源码安装的环境说明:
系统:CentOS 7.4
软件:nginx-1.17.0tar.gz
其他所需软件:openssl-1.0.2d.tar.gz、pcre-8.37.tar.gz、zlib-1.2.8.tar.gz
安装方式:源码编译安装
安装位置:/opt/data/nginx
下载地址:http://nginx.org/download
(1)安装环境准备,安装依赖包,创建安装目录
[root@localhost ~]# mkdir -p /opt/data/nginx/
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
[root@localhost ~]yum install gcc gcc-c++ make recp pcre-devel openssl openssl-devel -y
[root@localhost ~]# tar -xf nginx-1.17.0.tar.gz -C /usr/local/src/
(2).编译安装
[root@localhost nginx-1.14.0]# ./configure \
> --prefix=/opt/data/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-stream
[root@localhost nginx-1.14.0]# make &make install
(3)配置环境变量:
[root@localhost sbin]# ./nginx -t
[root@localhost sbin]# vim /etc/profile
export PATH=$PATH/opt/data/nginx/sbin #添加环境
[root@localhost sbin]# source /etc/profile
(4)Nignx配置:
[root@localhost nginx]# ln -s /opt/data/nginx/conf /etc/nginx
[root@localhost nginx]# vim nginx.conf
user nginx;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log off;
client_max_body_size 128M;
include /etc/nginx/conf.d/*.conf;
server{}
}
[root@localhost nginx]#mkdir /etc/nginx/conf.d
[root@localhost nginx]# mkdir /var/log/nginx
[root@localhost nginx]# chown -R nginx:nginx /var/log/nginx
(5)Nginx启动:
[root@localhost conf]#vim /lib/systemd/system/nginx.service #Centos7的启动。Centos6 的写在/etc/init.d 下面
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid -t是检查语法的意思
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
root@localhost conf]# systemctl enable nginx