nginx的安装与使用

本文详细介绍了如何在Linux环境下安装Nginx,包括从获取Nginx的下载链接,到通过SecureCRT连接Linux虚拟机,再到解压、配置、编译、安装Nginx的过程。在安装过程中,解决了缺少C编译器和YUM源的问题,并最终成功启动Nginx。同时,还提到了如何关闭防火墙以及验证Nginx是否正常运行的方法。
摘要由CSDN通过智能技术生成

windows

nginx下载地址:
链接:https://pan.baidu.com/s/13XXWmlcCCGw-eNu3NRFixg
提取码:i5df
直接将nginx解压即可使用(可以解压到任何路径下)。
具体的启动操作可以参考http://www.wuwenhui.cn/4570.html

linux

准备:linux虚拟机,SecureCRT
nginx下载地址:
链接:https://pan.baidu.com/s/1fZcVRhI1PUR6tkgdmrF9Vw
提取码:gayc

1、用SecureCRT连接虚拟机

1.1 查找虚拟机的ip地址

  • 在虚拟机的桌面空白处右击,选择在终端中打开
    在这里插入图片描述
  • 输入ifconfig,回车,找到虚拟机的ip地址
    在这里插入图片描述
    1.2 打开SecureCRT,创建连接
    在这里插入图片描述
  • 输入ip地址和用户名
    在这里插入图片描述
    在这里插入图片描述
  • 输入密码
    在这里插入图片描述
  • 连接成功
    在这里插入图片描述

2、安装niginx

  • 输入ps -ef | grep nginx,未安装nginxze则显示以下结果:
[root@Linux ~]# ps -ef | grep nginx
root      3086  3044  0 15:45 pts/1    00:00:00 grep nginx
  • 创建新用户并设置密码(注意,输入密码的过程中是看不见密码的,回车后会显示无效的密码,不必理会,重新输入新的密码确认即可):
[root@Linux ~]# useradd nginx
[root@Linux ~]# passwd nginx
更改用户 nginx 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
  • 这个时候,linux虚拟机的home目录下会自动生成nginx目录,然后我们上传nginx-1.10.0.tar.gz文件到目录:/home/nginx/
    Alt+P打开SFTP窗口,转到/home/nginx/目录,然后将nginx-1.10.0.tar.gz文件直接拖拽到SFTP窗口,即可完成上传(nginx-1.10.0.tar.gz所在路径最好没有中文)。
    在这里插入图片描述
  • 我们回到初始界面,到/home/nginx/目录下输入ll查看文件是否已成功上传
[root@Linux ~]# cd /home/nginx/
[root@Linux nginx]# ll
总用量 888
-rw-r--r--. 1 root root 908954 46 20:15 nginx-1.10.0.tar.gz
  • 将nginx-1.10.0.tar.gz解压
[root@Linux nginx]# tar -xvf nginx-1.10.0.tar.gz
  • 配置
[root@Linux nginx]# cd nginx-1.10.0
[root@Linux nginx-1.10.0]# ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx
checking for OS
 + Linux 2.6.32-573.el6.i686 i686
checking for C compiler ... not found

./configure: error: C compiler cc is not found

  • 执行命令后,会显示缺少C编译器(我这台虚拟机是新建的)。既然没有,那就下载yum install gcc。但是又报错了。
[root@Linux nginx-1.10.0]# yum install gcc
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
 Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/cache/yum/i386/6/base/mirrorlist.txt
错误:Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again
  • 因为我的虚拟机版本是centos6,而centos6的默认源在2012年-12月左右被官方搞掉了。
    修复:
[root@Linux nginx-1.10.0]# sed -i "s|enabled=1|enabled=0|g" /etc/yum/pluginconf.d/fastestmirror.conf
[root@Linux nginx-1.10.0]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@Linux nginx-1.10.0]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
102  1749  102  1749    0     0   2126      0 --:--:-- --:--:-- --:--:-- 10600

这个时候再执行yum install gcc命令,就不会报错了。在下载过程中,会出现确定吗?[y/N]:的提问,我们直接输入y+Enter即可。
事实上,我们还需要:pcre-deve和zlib-devel库,执行:
yum -y install pcre-devel
yum install -y zlib-devel
再次执行:./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx
当运行到最后,出现以下内容时,就证明配置成功了。


Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/usr/bin/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx/conf"
  nginx configuration file: "/opt/nginx/conf/nginx.conf"
  nginx pid file: "/opt/nginx/logs/nginx.pid"
  nginx error log file: "/opt/nginx/logs/error.log"
  nginx http access log file: "/opt/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
  • 编译安装
[root@Linux nginx-1.10.0]# make && make install
  • 启动nginx
    直接输入命令nginx即可启动。然后用ps -ef | grep nginx查看进程,如果进程没有三个,则可能是端口被占用了。可以用fuser -k 80/tcp关闭端口为80的进程再重新启动nginx。要注意不能多次启动nginx,否则会显示端口已被占用。
[root@Linux nginx-1.10.0]# nginx
[root@Linux nginx-1.10.0]# ps -ef | grep nginx
root       623     1  0 19:05 ?        00:00:00 nginx: master process nginx
nobody     624   623  0 19:05 ?        00:00:00 nginx: worker process
root       626  4278  0 19:05 pts/3    00:00:00 grep nginx
  • 关闭防火墙
    永久关闭:chkconfig iptables off
    暂时关闭:service iptables stop

  • 访问nginx
    如果安装成功了,在主机的浏览器中输入虚拟机的IP地址,我们就可以看到nginx的欢迎页面了。若看不到这个界面,可以尝试重启虚拟机,并启动nginx,再重新访问。
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值