新手在Linux下的编译安装Nginx的详细教程,以及安装过程中常见问题 | 您所在的位置:网站首页 › lunix启动nginx › 新手在Linux下的编译安装Nginx的详细教程,以及安装过程中常见问题 |
Linux下的Nginx详细安装教程(编译安装),以及安装过程中常见问题,适用新手。
网上的在linux下安装nginx的方法零零散散,在此进行全面的总结,编译安装的方法和会遇到的坑汇总成一篇文章。新手看这篇文章就够了。 快速入门也可以下拉到最后直接看总结的:正确的安装流程及命令。 此安装教程是在centOS 7下进行的,nginx版本为1.22.1 稳定版。 检测是否安装nginx: [root@localhost /]# nginx -v 下载nginx安装包: nginx官方下载地址:https://nginx.org/en/download.html Stable version 为最新稳定版本可以使用wget进行下载,centOS7默认没有安装wget命令,可以使用yum安装 安装wget: [root@localhost /]# yum -y install wget yum安装wget下载nginx安装包: [root@localhost usr]# wget -c https://nginx.org/download/nginx-1.22.1.tar.gz nginx安装包可在任意路径 (此处放置/usr文件夹下) 把nginx压缩包放置任意路径另外,也可以使用ssh软件sftp上传下载好的nginx安装包 使用tabby软件的sftp上传解压nginx压缩包: [root@localhost usr]# tar -zxvf nginx-1.22.1.tar.gz 解压nginx-1.22.1.tar.gz进入解压后的nginx-1.22.1文件夹,并执行./configure 进行自动配置(必须安装gcc环境和pcre,openssl两个依赖库) 以下是新手常见的两个问题: [root@localhost nginx-1.22.1]# ./configure (一). 如果没有安装gcc环境,在进行 ./configure 时会报错:./configure: error: C compiler cc is not found 如果没有gcc环境依赖gcc安装命令: [root@localhost nginx-1.22.1]# yum install gcc-c++ -y 查看gcc版本命令: [root@localhost nginx-1.22.1]# gcc -v gcc安装 ./configure自动配置执行make命令: [root@localhost nginx-1.22.1]# make (二). 如果没有安装pcre和openssl两个依赖库,在执行make命令时会报错:make: *** 没有规则可以创建“default”需要的目标“build”。 停止。 make: *** 没有规则可以创建“default”需要的目标“build”。 停止。pcre和openssl是安装nginx两个必需的依赖库! 安装命令: [root@localhost nginx-1.22.1]# yum -y install pcre-devel [root@localhost nginx-1.22.1]# yum -y install openssl-devel pcre依赖库安装: # yum -y install pcre-devel openssl依赖库安装: # yum -y install openssl-devel 安装完两个依赖后必须重新进行自动执行 ./configure然后重新执行make命令进行文件编译: [root@localhost nginx-1.22.1]# make make命令进行编译最后执行 make install进行安装: [root@localhost nginx-1.22.1]# make install make install进行最后的安装nginx安装完成! 查看nginx是否安装成功以及默认安装路径。 [root@localhost nginx-1.22.1]# whereis nginx 说明nginx已经安装成功,默认安装路径:/usr/local/nginx进入安装路径,查看nginx配置文件(以便之后nginx配置) [root@localhost nginx-1.22.1]# cd /usr/local/nginx/conf [root@localhost conf]# ll [root@localhost conf]# cat nginx.conf 查看nginx配置文件启动nginx:进入 /usr/local/nginx/sbin 路径,执行 ./nginx 启动nginx查看nginx是否启动(nginx的启动进程) [root@localhost sbin]# ps -ef | grep nginx nginx的启动进程通过浏览器访问nginx的web服务 http://192.168.111.131 (服务器的ip地址) 如果访问失败,一般是服务器的防火墙没有放行80端口。(如果使用的是阿里云服务器,还需要把服务器里安全组的80端口开启。)查看防火墙状态 [root@localhost ~]# systemctl status firewalld 防火墙状态(已开启)如果没有启动防火墙,开启命令: [root@localhost ~]# systemctl start firewalld 防火墙停止命令: [root@localhost ~]# systemctl stop firewalld 查看防火墙开启端口: [root@localhost ~]# firewall-cmd --list-all 查看防火墙端口设置防火墙的80端口: [root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent 开启防火墙的80端口 重新查看防火墙的端口,80端口已开启设置完端口后要重启防火墙: [root@localhost ~]# firewall-cmd --reload 设置防火墙端口,需要重启防火墙重新访问nginx的web服务: nginx成功开启web服务 总结: 正确的安装流程及命令:# 1.安装必需的依赖 [root@localhost usr]# yum install gcc-c++ [root@localhost usr]# yum -y install pcre-devel [root@localhost usr]# yum -y install openssl-devel # 2.下载安装包(任意路径) [root@localhost usr]# wget -c https://nginx.org/download/nginx-1.22.1.tar.gz # 3.解压安装包 [root@localhost usr]# tar -zxvf nginx-1.22.1.tar.gz # 4.进入解压后文件夹,自动配置 [root@localhost usr]# cd nginx-1.22.1 [root@localhost usr]# ./configure # 5.编译 [root@localhost usr]# make # 6.安装 [root@localhost usr]# make install nginx服务器常用命令:因为没有配置环境变量,所以启动和停止等需要在nginx的安装路径下(/usr/local/nginx/sbin)使用当前路径执行命令: ./ [root@localhost /]# cd /usr/local/nginx/sbin/ # 关闭nginx: [root@localhost sbin]# ./nginx -s stop # 安全退出nginx: [root@localhost sbin]# ./nginx -s quit # 启动nginx: [root@localhost sbin]# ./nginx # 重启nginx: [root@localhost sbin]# ./nginx -s reload # 检查nginx配置文件: [root@localhost sbin]# ./nginx -t # 查看nginx版本: [root@localhost sbin]# ./nginx -v # 查看nginx进程: [root@localhost sbin]# ps -ef | grep nginx nginx基本命令nginx的安装需要gcc环境,和pcre依赖库和openssl依赖库,最好在安装nginx服务之前把相应环境依赖配置好,避免安装时懵逼。 nginx的配置文件很重要,之后nginx服务器的相关配置都在此处完成。相关路径一定搞清楚。 nginx安装完成后,web服务访问失败,一般是防火墙的问题。根据情况开启相应端口即可。文章中已经详细说明 此安装教程是在centOS 7下进行的,nginx版本为1.22.1 稳定版。 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |