centOS 7 安装nginx的记录

最近在学习linux,就今天安装nginx时遇到的问题做下总结

下面是安装步骤以及常见问题的解决办法

1.安装依赖包

//一键安装上面四个依赖

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载并解压安装包

//创建一个文件夹

cd /usr/local

mkdir nginx

cd nginx

//下载tar包 wget http://nginx.org/download/nginx-1.13.7.tar.gz

tar -xvf nginx-1.13.7.tar.gz

3.安装nginx

//进入nginx目录

cd /usr/local/nginx

//进入目录

cd nginx-1.13.7

//执行命令 考虑到后续安装ssl证书 添加两个模块

./configure --with-http_stub_status_module --with-http_ssl_module

//执行make命令

make

//执行make install命令

make install

(如果遇到问题,跳转到9.(1))

4.启动nginx服务

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

如果遇到问题,跳转到9.(2))

4.配置nginx.conf

打开配置文件

vi /usr/local/nginx/conf/nginx.conf

将端口号改成8099(随便挑个端口),因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。

将localhost修改为你服务器的公网ip地址。

6.重启nginx

/usr/local/nginx/sbin/nginx -s reload

查看nginx进程是否启动:

ps -ef | grep nginx

7.若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口:

centOS6及以前版本使用命令: systemctl stop iptables.service

centOS7关闭防火墙命令: systemctl stop firewalld.service

关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口 :

开放8099端口:

firewall-cmd --zone=public --add-port=8099/tcp --permanent

查询端口号8099 是否开启:

firewall-cmd --query-port=8099/tcp

重启防火墙:

firewall-cmd --reload

随后访问该ip:端口 即可看到nginx界面。

8.安装完成一般常用命令

进入安装目录中,

命令: cd /usr/local/nginx/sbin

启动,关闭,重启,命令:

./nginx 启动

./nginx -s stop 关闭

./nginx -s reload 重启

9.遇到的问题

(1)所有操作都按照步骤来的但是在第3步 ,make和make install时报错

问题描述:

在安装Nginx输入make指令时报:

src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
cd .current_salt[0] = ~salt[0];
^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_copy_code’:
src/http/ngx_http_script.c:698:18: error:cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t )’ {aka ‘long unsigned int ()(struct )’} to ‘void ()(ngx_http_script_engine_t )’ {aka ‘void ()(struct *)’} [-Werror=cast-function-type]
code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
^

解决方案:

注意这里的nginx目录是自己具体安装目录

第一步,编辑ngx_user.c文件并注释掉报错代码

vim /usr/local/nginx/nginx-1.12.2/src/os/unix/ngx_user.c

找到下面这串代码并将其注释掉

cd.current_salt[0] = ~salt[0];

注释之后按Esc,:wq保存并退出

第二步,编辑Makefail文件去掉Makefail中的-Werror

vim /usr/local/nginx/nginx-1.12.2/objs/Makefile

找到-Werror删除(大概位置在进入文件的前几行)

按Esc 输入:wq保存并退出

再重新回到第三步make && make install 即可

(2)nginx解压后(或启动时)无sbin、启动nginx报-bash: ./nginx: 没有那个文件或目录;nginx -v 查看版本号失败

原因: 虽然通过su root切换到root用户,但是Nginx的压缩包和解压缩后的文件夹都是放在个人用户下的 应该cd到root根目录,然后再到usr/local/nginx下查看sbin目录

这两个问题的本质都是一样的,都是因为没有配置环境变量导致的

解决方案:

1.配置nginx环境变量

vim /etc/profile

根目录下运行 vim /etc/profile,在最下方添加如下代码

export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

2.执行source /etc/profile,重新加载配置文件

3.虽然没有SBIN,但是NGINX还是能启动(如果不能启动,请重启linux)

注意:一定要去/usr/local/nginx/sbin/下启动;

4.查看进程列表

grep -ef | grep nginx

出现nginx相关进程就是启动好了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值