-
添加nginx用户组及用户
groupadd nginx useradd nginx -g nginx
-
nginx使用C语言开发,在安装之前,需要先安装一些一些工具库
gcc-c++:nginx的源码编译需要该环境
pcre:Perl Compatible Regular Expressions,一个Perl库,可以解析正则表达,nginx中的http模块依赖此进行正则表达式的解析
pcre-devel:是pcre的一个二次元开发库
zlib:nginx需要依赖此库对http内容进行gzip
openssl:是一个安全套接字密码库,包括密码算法,证书,SSL协议等,而nginx支持http,https,而https是在ssl协议上传输的 -
安装以上的工具或类库
yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel
-
下载nginx
cd /usr/software/ wget -c https://nginx.org/download/nginx-1.15.2.tar.gz
-
解压并移动位置
tar -zxvf nginx-1.15.2.tar.gz mv nginx-1.15.2 /usr/nginx
-
编译安装
cd /usr/nginx # 如果需要去除Http头信息,加上 --add-module=/root/software/headers-more-nginx-module-v0.33 # 插件地址: https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz ./configure --with-http_stub_status_module --with-http_ssl_module --add-module=/root/software/headers-more-nginx-module-v0.33
注意:查看上面图片中的文件夹,如果不存在,最好手动创建一下,以免在安装时出现什么错误
mkdir /var/temp mkdir /var/temp/nginx mkdir /var/run/nginx/ make && make install cd /usr/local/nginx/
查看在/usr/local/nginx中存在conf,html等
-
nginx一些命令
进入安装目录:cd /usr/local/nginx/sbin/ 启动:./nginx,如果报nginx.pid no such file or directory,则需要在/var/run/nginx文件夹 指定nginx配置文件:./nginx -c /usr/local/nginx/conf/nginx.conf 停止:./nginx -s quit 重新加载:./nginx -s reload 或者 先停止再重启
-
验证
启动nginx,访问ip地址,如果出现welcome to nginx,则代表成功
-
如果有需要处理防火墙
关闭防火墙: systemctl stop firewalld.service 开启防火墙: systemctl start firewalld.service 关闭开机启动: systemctl disable firewalld.service 开启开机启动: systemctl enable firewalld.service
-
如果使用阿里云服务器,有个地方一定要注意,就是服务器实例的安全组一定要配置,将80端口配置进去,不然无法访问
-
开机启动
vi /etc/systemd/system/nginx.service 复制粘贴以下内容: [Unit] Description=nginx-server After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf PrivateTmp=true [Install] WantedBy=multi-user.target 然后: [root@localhost bin]# systemctl daemon-reload [root@localhost bin]# systemctl start nginx.service [root@localhost bin]# systemctl enable nginx.service
-
添加软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
-
开放防火墙
firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload firewall-cmd --list-ports firewall-cmd --list-service # 指定端口 firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
centos7 手动安装nginx
于 2018-08-21 20:53:20 首次发布