nginx 安装方法

安装Nginx

yum 安装

1. 安装 Nginx

yum 安装 nginx 非常简单,就输入一条命令即可。

yum -y install nginx   # 安装 nginx
yum remove nginx  # 卸载 nginx

使用 yum 进行 Nginx 安装时,Nginx 配置文件在 /etc/nginx 目录下。

2. 配置 Nginx 服务

systemctl enable nginx # 设置开机启动 
systemctl start nginx  # 启动 nginx 服务
systemctl stop nginx  # 停止 nginx 服务
systemctl restart nginx  # 重启 nginx 服务
systemctl reload nginx  # 重新加载配置,一般是在修改过 nginx 配置文件时使用。

源码包安装

1 依赖库安装

1.1. 安装 gcc 环境

yum -y install gcc gcc-c++ # nginx 编译时依赖 gcc 环境

1.2. 安装 pcre

yum -y install pcre pcre-devel # 让 nginx 支持重写功能

1.3. 安装 zlib

# zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩
yum -y install zlib zlib-devel 

1.4. 安装 openssl

# 安全套接字层密码库,用于通信加密
yum -y install openssl openssl-devel

以上安装完成后,进行 nginx 安装。

2 创建Nginx 运行普通用户

#新建nginx用户和nginx组
groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx

3 下载Nginx压缩包

cd /var/tmp/ && mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
mkdir -p /var/run/nginx

cd ~ && wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0

注意: 在使用下面这条configure参数配置时,一定要先把反斜杠“\”后面添加的注释文字去掉!!!

参考

./configure \
--prefix=/opt/service/nginx \         [Nginx安装目录]
--user=nginx \                        
--group=nginx  \				      
--sbin-path=/usr/sbin/nginx \         			 [Nginx的sbin目录]
--conf-path=/etc/nginx/nginx.conf \              [Nginx的配置文件]
--error-log-path=/var/log/nginx/error.log \      [Nginx的错误日志]
--http-log-path=/var/log/nginx/access.log \      [Nginx的访问日志]
--pid-path=/var/run/nginx/nginx.pid \            [Nginx的进程ID]
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \ 
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \ 
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--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-stream

编译

./configure --prefix=/opt/service/nginx --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client_body --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-stream

make && make install

加入开机启动

cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx servicedaemon
After=network.target

[Service]
Type=forking
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
#PrivateTmp=true

[Install]
WantedBy=multi-user.target


[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
posted @ 2020-08-25 17:38  Blue_Smile  阅读(693)  评论(0编辑  收藏  举报