CentOS 7安装 nginx

CentOS 7 安装 nginx 教程

安装 nginx 编译所需的 lib 库

1
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel

查看 pcre(正则库)版本

1
pcre-config --version

进入编译目录

1
cd /usr/local/src

官网下载最新的 nginx(stable version 稳定版)

1
wget http://nginx.org/download/nginx-1.24.0.tar.gz

解压 nginx 压缩包

1
tar -zxvf nginx-1.24.0.tar.gz

进入解压目录

1
cd  nginx-1.24.0

运行配置脚本

--prefix参数指定nginx安装的目录,默认安装在/usr/local/nginx

  • 默认安装
1
./configure --prefix=/usr/local/nginx
  • 或添加一些模块安装
    添加 https,stream 等模块
1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_v2_module

编译安装 nginx

1
make && make install

将 nginx 执行命令软链接到/usr/bin

1
ln -s /usr/local/nginx/sbin/nginx /usr/bin

启动 nginx

1
nginx

设置开机自启动

1
2
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

执行 nginx -h 查看相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
nginx -h

nginx version: nginx/1.24.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

查看 nginx 安装目录

1
whereis nginx

vim语法高亮

1
cp -r /usr/local/src/nginx-1.24.0/contrib/vim/* /usr/share/vim/vimfiles

自定义conf目录,例如/usr/local/nginx/conf.d

1
2
3
4
vim /usr/share/vim/vimfiles/ftdetect/nginx.vim

# 添加以下内容
au BufRead,BufNewFile */usr/local/nginx/conf.d/* set ft=nginx

nginx常用配置

  • www跳转m
1
2
3
if ($http_user_agent ~* '(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Opera)') {
rewrite ^.+ https://m.test.com/$uri;
}
作者

风静花犹落

发布于

2020-05-22

更新于

2023-11-21

许可协议