工欲善其事,必先利其器
方式一:源码安装与卸载
安装
安装编译所需的一些依赖库
$ yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel gd gd-devel
下载安装包 下载地址 找到想要安装的版本,鼠标右键复制链接
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
解压
$ tar xzvf nginx-1.16.1.tar.gz
进入解压后的目录,配置
Nginx
$ cd nginx-1.16.1 $ ./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_v2_module
执行编译并安装
$ make $ make install
验证与启动
$ /usr/local/nginx/sbin/nginx -V $ /usr/local/nginx/sbin/nginx
其他命令
$ /usr/local/nginx/sbin/nginx -s reload # 重启 $ /usr/local/nginx/sbin/nginx -s stop # 停止 $ /usr/local/nginx/sbin/nginx -t # 测试配置文件
(可选)创建软链接
$ ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx $ nginx # 启动 $ nginx -t # 测试配置文件 $ nginx -s reload # 重启 $ nginx -s stop # 停止
(可选)开机自启动
$ vim /usr/lib/systemd/system/nginx.service # 添加以下内容 [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=root
# 相关命令 $ systemctl status nginx # 查看状态 $ systemctl start nginx # 启动 $ systemctl stop nginx # 停止 $ systemctl reload nginx # 重新加载 $ systemctl enable nginx # 设置开机启动 $ systemctl disable ngixn # 关闭开机启动
卸载
检查运作状态并停止服务
$ ps -ef | grep nginx $ nginx -s stop
编译安装卸载方便,删除安装目录、日志目录和临时目录
$ rm -rf /usr/local/nginx $ rm -rf /var/log/nginx $ rm -rf /var/tmp/nginx
相比通过系统的软件包管理器安装,通过源码方式安装有以下好处:
- 可以指定安装目录和相关文件路径,包管理器方式是安装到默认位置。
- 灵活地编译指定模块(如:
--with-http_v2_module
),包管理器方式的安装默认带有的指定模块。
方式二:RPM
方式安装与卸载
安装
检查是否已安装
Nginx RPM
包$ rpm -qa | grep nginx # 如果已经安装,先卸载 $ rpm -e nginx
下载
Nginx RPM
包
前往 下载地址 找到需要的版本,如nginx-1.16.1-1.el7.ngx.x86_64.rpm
$ wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.1-1.el7.ngx.x86_64.rpm
执行
RPM
安装命令$ rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm
默认安装位置在
/etc/nginx/
目录下。启动
$ systemctl start nginx # 其他命令不再赘述
卸载
查看是否已安装
$ rpm -qa | grep nginx
停止
Nginx
服务$ systemctl stop nginx
卸载
$ rpm -e nginx
删除文件
$ rm -rf /etc/nginx/
其他
打开防火墙端口
CentOS 7.x
使用的防火墙firewalld
默认是关闭http
服务的,需打开80
端口
$ firewall-cmd --zone=public --permanent --add-service=http
$ firewall-cmd --reload
$ firewall-cmd --list-service # 查看防火墙打开的所有服务
反向代理出现Permission denied
在启用了SELinux
的服务器中,使用反向代理时需要打开网络访问权限。在默认情况下,SELinux
只允许部分端口提供对外的服务(通常情况下默认端口有80
、81
、443
、488
、8008
、8009
、8443
、9000
),如果反向代理的是其他端口,就会出现Permission denied
的错误。
可以通过以下命令查看当前允许的端口
$ semanage port --list | grep http_port_t
添加开放的端口规则
$ semanage port --add --type http_port_t --proto tcp 6000
后续有其他内容点,将持续更新...
暂无评论