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.g
nginx其他版本下载地址:https://nginx.org/download/
3.安装nginx
//进入nginx目录
cd /usr/local/nginx
//执行命令
./configure
//执行make命令
make
//执行make install命令
make install
4.配置nginx.conf
# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
将端口号改成8089,因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。localhost修改为你服务器ip地址。
5.启动nginx
/usr/local/nginx/sbin/nginx -s reload
查看nginx进程是否启动:ps -ef | grep nginx
问题1:/nginx -s reload 启动nginx时可能会报问题:环境问题
解决方法:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
使用nginx -c的参数指定nginx.conf文件的位置
[root@localhost nginx]# cd logs/
[root@localhost logs]# ll
总用量 12
-rw-r--r-- 1 root root 1246 12月 9 18:10 access.log
-rw-r--r-- 1 root root 516 12月 10 15:39 error.log
-rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid
看nginx.pid文件已经有了。
问题2:
说明服务器的80端口是打不开的。
因为我使用的linux系统版本是CentOS7,所以可以在服务器中执行如下命令来验证
firewall-cmd --query-port=80/tcp
显然80端口没有开启。
下面我们开启80端口:
firewall-cmd --add-port=80/tcp --permanent
#重启防火墙
systemctl restart firewalld
--permanent #永久生效,没有此参数重启后失效
6.若想使用外部主机连接上虚拟机访问端口192.168.131.2,需要关闭虚拟机的防火墙:
centOS6及以前版本使用命令: systemctl stop iptables.service
centOS7关闭防火墙命令: systemctl stop firewalld.service
随后访问该ip即可看到nginx界面。
7.访问服务器ip查看
安装完成一般常用命令
进入安装目录中,
命令: cd /usr/local/nginx/sbin
启动,关闭,重启,命令:
./nginx 启动
./nginx -s stop 关闭
./nginx -s reload 重启
8.配置nginx开机自启动
vim /etc/rc.d/rc.local