Linux 系统安装 Nginx
Linux 系统安装 Nginx
CentOS 7 Nginx 源代码编译安装
# 官网下载最新版
https://nginx.org/en/download.html
# 系统安装必备组件:
yum install gcc gcc-c++ pcre pcre-devel zlib-devel openssl-devel openssl -y
# 添加Nginx用户:
useradd nginx -s /sbin/nologin -M
# 建立对应目录:
mkdir -p /var/cache/nginx/client_temp
yum -y install unzip
# 源码解压缩配置:
确保 nginx-rtmp-module-master.zip 和 nginx-1.22.1.tar.gz 在同一目录下
unzip nginx-rtmp-module-master.zip
tar -xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --add-module=../nginx-rtmp-module-master --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
# 快速测试上一步运行是否出错(正确数字为0,错误为其他数字):
echo $?
0
# 编译安装
make && make install
# 快速测试上一步运行是否出错(正确数字为0,错误为其他数字):
echo $?
0
# 查看Nginx版本及安装参数:
nginx -V
Ubuntu 安装 Nginx
# 系统软件源安装
apt install nginx -y
# 开机启动
systemctl enable nginx
# 关闭服务
systemctl stop nginx
# 启动服务
systemctl start nginx
# 查看服务状态
systemctl status nginx
# 重启服务
systemctl restart nginx
# 创建日志目录及文件
mkdir -p /usr/share/nginx/logs/
touch /usr/share/nginx/logs/error.log
# 保存原始配置文件
mv /etc/nginx/nginx.conf /etc/nginx/nginx.confori
# 过滤注释行
grep -Ev '^$|#' /etc/nginx/nginx.conf
Docker 运行 Nginx
# 拉取镜像
docker pull nginx
# 创建目录及修改权限
mkdir -p /root/nginx_conf/conf.d
mkdir /root/nginx_logs
mkdir /root/nginx_web
chmod -R 777 /root/nginx_conf
chmod -R 777 /root/nginx_logs
chmod -R 777 /root/nginx_web
# 启动 Nginx
docker run -itd --name nginx -p 80:80 -p 443:443 -v /root/nginx_web:/var/www/html -v /root/nginx_conf:/etc/nginx -v /root/nginx_logs:/var/log/nginx nginx