nginx编译安装
个人部署服务步骤总结
编译nginx
wget https://nginx.org/download/nginx-1.24.0.tar.gz #官网下载安装包
tar xfz nginx-1.24.0.tar.gz -C /usr/local/ #解压
cd /usr/local/ && mv nginx-1.24.0/ nginx #改名
yum -y install gcc gcc-c++ #nginx需要的编译器和解释器
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream #编译安装并且指定数据和模块
make && make install #编译,安装
创建mkdir -p /tmp/nginx #用于存储客户端请求体(request body)的临时目录
mkdir /usr/local/nginx/logs #创建日志目录
mkdir /etc/nginx/conf.d #创建子目录文件
修改/etc/nginx/nginx.conf配置文件:复制下面
worker_processes auto; #自动根据cpu分配work进程
error_log logs/error.log; #日志存放目录
error_log logs/error.log notice;
error_log logs/error.log info;
pid /var/run/nginx.pid; #pid进程
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf; #子配置目录
charset utf-8; #指定语言为utf8
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on; #开启压缩功能
}
(可选)设置子配置目录,访问初始页面
vim /etc/nginx/conf.d/test.conf
server {
listen 80; #监听端口
server_name localhost; 本机ip
location / {
root html;
index index.html index.htm; web文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
启动方式二选一
1:
echo 'NGINX_HOME=/usr/local/nginx' >> /etc/profile
echo 'PATH=$NGINX_HOME/sbin:$PATH' >> /etc/profile
source /etc/profile #将nginx设置环境变量,可以直接使用nginx启动
2:
vim /etc/systemd/system/nginx.service
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
Restart=on-failure
[Install]
WantedBy=multi-user.target
添加到system管理
systemctl daemon-reload #加载配置
可以使用systemctl进行启动关闭