Nginx-Centos安装简单教程

427 9~12 min

Nginx-Centos安装简单教程

按需下载Nginx版本

http://nginx.org/download/ 下载文件结尾为.tar.gz

  # 进入自己的目录
  cd /usr/local/ 
  # 将选择的版本进行下载
  wget http://nginx.org/download/nginx-1.23.0.tar.gz
  # 解压nginx压缩包到nginx 文件夹
  tar -zxvf nginx-1.23.0.tar.gz  nginx

编译Nginx

先安装编译Nginx需要的环境

//安装gcc
yum install gcc-c++
 
//安装PCRE pcre-devel
yum install -y pcre pcre-devel
 
//安装zlib
yum install -y zlib zlib-devel
 
//安装Open SSL
yum install -y openssl openssl-devel

编译需要的模块

# 进入Nginx目录
cd /usr/local/nginx
# 执行编译命令 可以按需编译需要的模块
./configure \
--prefix=/usr/local/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.pid \
--lock-path=/var/run/nginx.lock \ 
--user=root \
--group=root \
--with-http_gzip_static_module \
--with-http_stub_status_module \  
--with-http_ssl_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_addition_module \  
--with-http_dav_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_v2_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_secure_link_module \
--with-stream \
--with-stream_ssl_module

编译完成执行安装命令

make && make install

运行Nginx

Nginx可执行文件位置编译完成在 /usr/local/nginx/sbin 配置文件的位置在/usr/local/nginx/conf/nginx.conf

# 启动NGINX
/usr/local/nginx/sbin/nginx   -c /usr/local/nginx/conf/nginx.conf

查看Nginx启动状态

# 运行netstat命令查看nginx是否监听80端口
[root@localhost local]# netstat -lnpt 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      86795/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      997/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1232/master         
tcp6       0      0 :::22                   :::*                    LISTEN      997/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1232/master      

将Nginx作为系统服务实现开机自启动

  • 创建nginx.service服务文件

在/etc/systemd/system目录下创建nginx.service文件:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 启动和重新加载nginx服务
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
  • 检查nginx服务状态
systemctl status nginx.service
  • 命令启动与停止
systemctl stop nginx.service
systemctl restart nginx.service

将Nginx的可执行文件注册时全局

  • 查找Nginx可执行文件的目录:
which nginx
/usr/local/nginx/sbin/ngin
  • 编辑profile文件添加PATH

对bash shells,编辑/etc/profile文件:

vim /etc/profile
# 添加nginx可执行文件的路径
export PATH=/usr/local/nginx/sbin:$PATH
vim /etc/zshrc  
# 添加同样的PATH内容。
export PATH=/usr/local/nginx/sbin:$PATH
  • 让修改生效
source /etc/profile
  • 检查是否生效
    此时你可以从任何路径下执行:
nginx -t