环境
系统:CentOS 6.9 或 CentOS 7.4
软件:nginx-1.12.1.tar.gz
Nginx安装
下载源码包
地址:https://nginx.org/-
添加账户
# useradd nginx
-
安装依赖包
# yum -y install gcc automake pcre-devel zlib-devel openssl-devel
-
编译安装
# tar -zxvf nginx-1.12.1.tar.gz # cd nginx-1.12.1 # ./configure \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-stream # make # make install
-
配置环境变量
# vim /etc/profile export PATH=/usr/local/nginx/sbin:$PATH # source /etc/profile
Nginx配置
- 编辑配置文件
# ln -s /usr/local/nginx/conf /etc/nginx
# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time "$http_x_forwarded_for"';
access_log off;
sendfile on;
keepalive_timeout 65;
gzip on;
client_max_body_size 128M;
include conf.d/*.conf;
}
stream {
include conf.d/*.stream;
}
- 创建文件夹
# mkdir /etc/nginx/conf.d # mkdir /var/log/nginx # chown -R nginx:nginx /var/log/nginx
Nginx启动
CentOS 6.9
-
添加启动服务
# wget https://raw.githubusercontent.com/yuantong127/ansible/master/roles/nginx-install/files/nginxd # mv nginxd /etc/init.d/nginxd # chmod +x /etc/init.d/nginxd
-
启动
# chkconfig --add nginxd # chkconfig nginxd on # /etc/init.d/nginxd start
CentOS 7.4
-
添加启动服务
# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
-
启动
# systemctl enable nginx.service # systemctl restart nginx.service