欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux下安装Nginx

程序员文章站 2022-05-17 18:10:41
...

一、准备

Nginx版本:nginx-1.7.7.tar.gz;请自行到官网下载,本文是基于CentOS安装的。

二、安装步骤

①新建一个ant用户
[aaa@qq.com /]# useradd ant -d /ant
设置新用户密码,出现以下提示信息,按照提示,进行操作
[aaa@qq.com /]# passwd ant
更改用户 ant 的密码 。
新的 密码:
无效的密码: 它基于字典单词
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新

创建一个目录ant,并设置权限

[aaa@qq.com /]# mkdir /ant
[aaa@qq.com /]# chown ant:ant /ant/ -R

nginx的一些模块依赖一些lib库,在安装nginx之前,须先安装这些lib库

[aaa@qq.com /]# yum -y install gcc-c++  
[aaa@qq.com /]# yum -y install pcre pcre-devel  
[aaa@qq.com /]# yum -y install zlib zlib-devel  
[aaa@qq.com /]# yum -y install openssl openssl—devel

上传nginx的文件包,并解压安装

[aaa@qq.com /]# mkdir /usr/local/src/nginx
[aaa@qq.com /]# cd /usr/local/src/nginx
[aaa@qq.com nginx]# rz
z waiting to receive.**B0100000023be50
[aaa@qq.com nginx]# ls -lrt
总用量 804
-rw-r--r--. 1 root root 819949 11月  9 2016 nginx-1.7.7.tar.gz
[aaa@qq.com /]# tar -xvf nginx-1.7.7.tar.gz
[aaa@qq.com /]# cd nginx-1.7.7

将nginx安装到ant

[aaa@qq.com /]# mkdir -p /ant/soft/nginx
[aaa@qq.com /]# ./configure --prefix=/ant/soft/nginx --user=ant --group=ant
[aaa@qq.com /]# make  
[aaa@qq.com /]# make install
进入ant目录下面,你会发现所有的文件目录都属于root用户,这时候需要修改/ant下文件的所属,执行以下命令后,您会发现所属已经改变
[aaa@qq.com /]# cd /ant
[aaa@qq.com ant]$ ls -lrt
drwxr-xr-x. 3 root root 4096 6月  29 06:49 soft   #属于root
[aaa@qq.com root]$ cd soft
[aaa@qq.com soft]$ ls -lrt
drwxr-xr-x. 11 root root 4096 6月  29 07:00 nginx   #属于root
[aaa@qq.com /]# chown ant:ant /soft/ -R
[aaa@qq.com ant]# ls -lrt
drwxr-xr-x. 3 ant ant 4096 6月  29 06:49 soft  #属于ant
[aaa@qq.com ant]# cd soft
[aaa@qq.com soft]# ls -lrt
drwxr-xr-x. 11 ant ant 4096 6月  29 07:00 nginx   #属于ant

切换ant用户,要以普通用户身份启动nginx,方便管理,进入/ant/soft/nginx/sbin目录下面启动nginx

[aaa@qq.com sbin]# cd /ant/soft/nginx/sbin
[aaa@qq.com sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)   #权限不够,非root用户不能占用80端口,所以使普通用户以root身份启动nginx
[aaa@qq.com sbin]$ su root   #切换回root用户
密码:
[aaa@qq.com sbin]# cd /ant/soft/nginx/sbin
[aaa@qq.com sbin]# chown root nginx
[aaa@qq.com sbin]# chmod u+s nginx   #让普通用户可以以root的方式启动nginx
[aaa@qq.com sbin]# su ant
[aaa@qq.com sbin]$ ls
nginx
[aaa@qq.com sbin]$ ./nginx

检查nginx是否开启,使用ps管道命令

[aaa@qq.com sbin]# ps -ef|grep nginx
root      4795     1  0 07:34 ?        00:00:00 nginx: master process ./nginx
501       4796  4795  0 07:34 ?        00:00:00 nginx: worker process
root      4798  4774  0 07:34 pts/0    00:00:00 grep nginx
[aaa@qq.com sbin]# 

由上面可知nginx服务已经开启,进入到前台访问,发现访问不了,因为防火墙没有打开,进行如下操作:

[aaa@qq.com /]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[aaa@qq.com /]# /etc/rc.d/init.d/iptables save
[aaa@qq.com /]# /etc/init.d/iptables status

重新启动nginx

[aaa@qq.com /]# ./nginx -s restart

三、效果

看到如下界面说明启动成功,访问链接http://192.168.136.100:80,默认80可以去掉。

Linux下安装Nginx

版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。