安装nginx之前要先安装依赖包
yum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel
1.gcc安装:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装 2.PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。 nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库 3.zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。 4.OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
nginx下载与安装
直接下载.tar.gz安装包,下载地址:https://nginx.org/en/download.html
把下载的包放到/usr/local/nginx目录,然后解压安装到nginx-1.12.0当前目录,make编译安装
[root@yoyo sbin]# cd ~ [root@yoyo ~]# cd /usr/local/ [root@yoyo local]# mkdir nginx [root@yoyo local]# cd nginx
[root@yoyo nginx]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz(这个指令暂时用不了可进入网页直接下载) [root@yoyo nginx]# tar -zxvf nginx-1.12.0.tar.gz [root@yoyo nginx]# cd nginx-1.12.0
# 安装到当前目录 [root@yoyo nginx]# ./configure
(记住进入到nginx-1.12.0这里再启动不然会提示不是目录)
(如果提示权限不足更改属性):
chmod -R 777 ./configure
[root@yoyo nginx]# make [root@yoyo nginx]# make install 到此为止环境已经安装好,接下来启动nginx服务 [root@yoyo nginx]# cd /usr/local/nginx/sbin/ [root@yoyo nginx]# ./nginx
开放80端口: firewall-cmd --add-port=80/tcp --permanent firewall-cmd --add-port=80/udp --permanent firewall-cmd --reload
启动服务后,nginx默认是在80端口启动的,在浏览器输入http:// 192.168.80.129:80/端口默认可以省略),能正常访问到页面,说明服务启动成功
相关指令 先cd到/usr/local/nginx/sbin/ 1.启动服务 ./nginx 2.停止服务,此方式停止步骤是待nginx进程处理任务完毕进行停止。 ./nginx -s stop 3.退出服务,此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。 ./nginx -s quit 4.重新加载,当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx, 使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效 ./nginx -s reload 5.查询nginx进程 ps aux|grep nginx
修改nginx启动端口
如果80端口之前已经使用过了,可以修改nginx的服务端口,先cd到/usr/local/nginx/conf目录
为了保险起见,编辑前先备份下原来的文件:cp nginx.conf nginx.conf.bak
vim打开后,找到服务端口listen 80这段,输入键盘上i键后编辑,改成81
编辑完成后按Esc键,输入:wq保存退出 修改后重新加载下配置文件 cd /usr/local/nginx/sbin/ ./nginx -s reload 最后,开放81端口 开放81端口: firewall-cmd --add-port=81/tcp --permanent firewall-cmd --add-port=81/udp --permanent firewall-cmd --reload