方式一:yum安装
安装:
yum install nginx
启用并启动Nginx服务:
$ sudo systemctl enable nginx $ sudo systemctl start nginx
停止服务:
$ sudo systemctl stop nginx
要验证服务是否正在运行,检查其状态:
$ sudo systemctl status nginx
方式二:自定义目录安装
1.安装工具和库
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# PCRE是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式
# zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip
2.下载并解压nginx
wget -c https://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz
3.编译与安装nginx
1,configure
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre
#--prefix 指定安装路径
#--with-http_stub_status_module 允许查看nginx状态的模块
# --with-http_ssl_module 支持https的模块
执行成功后显示的文件路径:
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
2,编译并安装
make && make install
4.启动nginx
进入到安装nginx目录下面的sbin
启动命令
./nginx
打开浏览器访问你的IP地址,显示此页面说明nginx启动成功。
其他命令:
./nginx -s quit: (温和)此方式停止步骤是待nginx进程处理任务完毕进行停止。 ./nginx -s stop: (强硬)此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s reload 重启nginx(不推荐此方法,推荐先停止在启动)