一、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、首先要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
1、下载 PCRE 安装包,下载地址: Download pcre-8.35.tar.gz (PCRE)
[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解压安装包:
[root@bogon src]# tar zxvf pcre-8.35.tar.gz
3、进入安装包目录
[root@bogon src]# cd pcre-8.35
4、编译安装
[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install
5、查看pcre版本
[root@bogon pcre-8.35]# pcre-config --version
三、安装Nginx
1、下载 Nginx,下载地址:nginx: download
[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
2、安装包
[root@bogon src]# tar zxvf nginx-1.6.2.tar.gz
3、进入安装包目录
[root@bogon src]# cd nginx-1.6.2
4、编译安装
[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@bogon nginx-1.6.2]# make
[root@bogon nginx-1.6.2]# make install
5、查看nginx版本
[root@bogon nginx-1.6.2]# /usr/local/nginx/sbin/nginx -v
6、设置软链接(选配)
$ ln -s /usr/local/nginx/sbin/nginx /usr/bin/
# 设置软链后,可以尝试在其他目录使用以下命令看是否成功
$ nginx -v
四、Nginx常用命令
1、cd /usr/local/nginx/sbin/ //进入目录
2、./nginx //启动
3、./nginx -s stop //停止
4、./nginx -s quit //安全退出
5、./nginx -s reload //重载配置文件(修改了配置文件需要执行此命令 比较常用)
6、ps aux|grep nginx //查看ngnix进程
五、安装Nginx到系统服务
1、创建服务脚本
vi /usr/lib/systemd/system/nginx.service
2、脚本内容
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3、重新加载系统服务
systemctl daemon-reload
4、启动Nginx服务
systemctl start nginx.service
5、设置开机启动Nginx服务
systemctl enable nginx.service
六、关于防火墙
1、关闭防火墙
systemctl stop firewalld.service
2、进制防火墙开机启动
systemctl disable firewalld.service
3、放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
4、重启防火墙
firewall-cmd --reload