Linux---安装nginx
1.安装所需环境(依赖包)
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel //一键安装
详解:
分开安装命令:
yum install gcc-c++ //安装gcc
yum install -y pcre pcre-devel //安装PCRE pcre-devel
yum install -y zlib zlib-devel //安装zlib
yum install -y openssl openssl-devel //安装Open SSL
2.下载并解压安装包
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz //下载tar包
tar -xvf nginx-1.13.7.tar.gz
3.安装Nginx
cd /usr/local/nginx //进入Nginx目录
cd nginx-1.13.7 //进入目录
./configure --with-http_stub_status_module --with-http_ssl_module //执行命令,考虑到后续安装ssl证书,添加两个模块
make //执行make命令
make install //执行 make install 命令
4.启动Nginx服务
cd /usr/local/nginx/sbin/ //进入nginx目录
./nginx //启动nginx
或者/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5.配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf //将端口号修改为8089 ,localhost改成你的服务器公网ip
注:端口因可能apeache占用80端口,apeache端口尽量不修改,我们改nginx的端口
6.重启nginx
/usr/local/nginx/sbin/nginx -s reload
ps -ef | grep nginx //查看nginx进程是否启动
7.外部主机访问nginx,需关闭防火墙或开放nginx服务端口(nginx.conf配置的端口)
centos6以下命令: systemctl stop iptables.service
centos7 命令: systemctl stop firewalld.service
firewall-cmd --zone=public --add-port=80/tcp --permanent //开放端口操作:
firewall-cmd --query-port=80/tcp //查询端口是否开启
firewall-cmd --reload //重启防火墙
8.通过服务器ip访问主页+端口(80端口默认不写)
9.设置开机启动
vim /etc/rc.local //编辑
/usr/local/nginx/sbin/nginx //最底部增加这一行
10. 常用命令
cd /usr/local/ngin/sbin //进入安装目录
./nginx //启动
./nginx -s stop //关闭
./nginx -s reload //重启
---------------------------------------------------------------------
问题收集:
1.启动nginx报错,系统提示 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 所有页面提示502错误
a.查看ngi端口 netstat -tnulp |grep nginx
b.杀死被占端口 kill -9 端口号
c.再次查看nignx端口 netstat -tnulp |grep 80
d.重启nginx