跳到主要内容
方的代码随记
返回

CentOS / Ubuntu 搭建 Nginx

更新于:

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 foundyum -y install gcc gcc-c++ autoconf automake make
the HTTP rewrite module requires the PCRE libraryyum install pcre + yum install pcre-devel
the HTTP gzip module requires the zlib libraryyum 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 等路径。


分享这篇文章:

上一篇
Nginx 性能优化
下一篇
Nginx 配置文件结构详解