CentOS 源码编译
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zxvf nginx-1.12.1.tar.gz
./configure --prefix=/usr/local/nginx
make && make install
configure 常见报错
| 报错 | 解决办法 |
|---|---|
C compiler cc is not found | yum -y install gcc gcc-c++ autoconf automake make |
the HTTP rewrite module requires the PCRE library | yum install pcre + yum install pcre-devel |
the HTTP gzip module requires the zlib library | yum install -y zlib + yum install -y zlib-devel |
安装后收尾
/usr/local/nginx/sbin/nginx -t # 测试配置
mkdir -p /data/web/www /data/logs/nginx
groupadd www
useradd -g www www
cp nginx.conf /usr/local/nginx/conf/
/usr/local/nginx/sbin/nginx # 启动
更多编译参数(模块选择、pcre 依赖)见 源码编译 Nginx 与 PHP。
Ubuntu 包管理器安装
su # 或 sudo
apt-get install nginx
service nginx start
安装后的位置:
| 路径 | 用途 |
|---|---|
/usr/sbin/nginx | 主程序 |
/etc/nginx | 配置文件 |
/usr/share/nginx | 静态文件 |
/var/log/nginx | 日志 |
Linux 开机自启动
保存为 /etc/systemd/system/nginx.service,然后 chmod 755:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx
源码安装的路径是
/usr/local/nginx/sbin/nginx,写 service 文件时注意替换ExecStart等路径。