1 二进制安装
Ubuntu等系统: sudo apt-get install nginx
centos系统: sudo yum install nginx
2 编译安装
2.1 环境准备
准备好make、wget、g++等软件。 yum install -y gcc-c++ make wget
2.2 下载软件
下载如下编译需要用的源代码。 下载openssl主要用于ssl模块加密,支持htps: wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
下载pcre来实现对地址重定向,地址重写功能和localtion指令以及正则表达式的支持: wget https://ftp.exim.org/pub/pcre/pcre-8.43.tar.gz
下载zlib gzip压缩模块: wget https://zlib.net/fossils/zlib-1.2.11.tar.gz
下载nginx源码: wget "https://nginx.org/download/nginx-1.18.0.tar.gz" --user-agent="Mozilla/5.0 (X11;U;Linux i686;en-US;rv:1.9.0.3) Geco/2008092416 Firefox/3.0.3"
2.3 解压文件
ls *.tar.gz | xargs -n1 tar xzvf
2.4 编译安装
编译选项:
2.4.1 编译配置
./configure \
--with-openssl=../openssl-1.0.2s \
--with-pcre=../pcre-8.43 \
--with-zlib=../zlib-1.2.11 \
--with-http_ssl_module \
--with-http_v2_module
2.4.2 检查
输出下面内容,表示正常
Configuration summary
+ using PCRE library: ../pcre-8.43
+ using OpenSSL library: ../openssl-1.0.2s
+ using zlib library: ../zlib-1.2.11
nginx path prefix: "/home/admin/nginx"
nginx binary file: "/home/admin/nginx/sbin/nginx"
nginx modules path: "/home/admin/nginx/modules"
nginx configuration prefix: "/home/admin/nginx/conf"
nginx configuration file: "/home/admin/nginx/conf/nginx.conf"
nginx pid file: "/home/admin/nginx/logs/nginx.pid"
nginx error log file: "/home/admin/nginx/logs/error.log"
nginx http access log file: "/home/admin/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
2.4.3 编译
make
如果报错,请确定gcc-c++ make软件是否全局安装。
2.4.4 安装
make install
2.5 安装完成
默认安装目录/usr/local/nginx
2.6 配置nginx.conf
路径位于/usr/local/nginx/conf/nginx.conf:
#指定线程的用户 需创建个非root权限的用户
user www;
worker_processes 1;
# 指定用户线程
error_log /home/www/wwwlogs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定pid文件生成路径 和systemd配置文件的位置对应
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" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 指定多站点配置文件的路径
include /home/wwwconfig/*.conf;
}
2.7 配置systemd
创建配置文件: vim /usr/lib/systemd/system/nginx.service
增加内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重载systemd systemctl daemon-reload
2.8 启动nginx
注意如果监听的端口低于1024需要root用户启动 systemctl start nginx
评论