1、将压缩包上传到服务器
nginx-1.21.3.tar.gz压缩包,上传到/usr/local/目录
2、安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
3、编译安装
tar -zxvf nginx-1.21.3.tar.gz
进入解压后目录执行编译
cd nginx-1.21.3
./configure --with-http_ssl_module
执行make install 命令
make install
4、启动
进入实际安装目录,看看,并在其sbin目录下执行启动nginx
cd /usr/local/nginx/sbin
运行命令启动Nginx
./nginx
5、Nginx加入systemctl方式自启动
1.建立服务文件
vim /usr/lib/systemd/system/nginx.service
新建文件,把下面文件内容放到文件中
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
2. 文件设为755权限并使文件生效
chmod 755 /usr/lib/systemd/system/nginx.service systemctl daemon-reload
3.设置开机启动
systemctl enable nginx.service
4.设置upload路径权限,使上传图片可读
chmod 755 -R /upload
配置nginx.conf
只有返回类似 Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service提示才能够确定设置成功。如果不是请检查上面的文件
5. 其他可能用到的命令
systemctl is-enabled servicename.service #查询服务是否开机启动 systemctl enable *.service #开机运行服务 systemctl disable *.service #取消开机运行 systemctl start *.service #启动服务 systemctl stop *.service #停止服务 systemctl restart *.service #重启服务 systemctl reload *.service #重新加载服务配置文件 systemctl status *.service #查询服务运行状态 systemctl --failed #显示启动失败的服务
6、Nginx配置文件
文件位置在(前提是按照文档安装,即Nginx安装目录下的conf文件夹)
/usr/local/nginx/conf
- 单机服务及静态资源
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/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; #} #autoindex on; location / { root /www/80; #静态资源路径 index index.html index.htm; } location /upload/ { root /; #文件上路径 # index index.html index.htm; } location /api { proxy_pass http://127.0.0.1:8009; #后端接口服务 } location /swagger-ui.html { proxy_pass http://127.0.0.1:8009; index index.html index.htm; } location /webjars { proxy_pass http://127.0.0.1:8009; index index.html index.htm; } location /swagger-resources { proxy_pass http://127.0.0.1:8009; index index.html index.htm; } location /v2 { proxy_pass http://127.0.0.1:8009; 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; # } #} }
- 多机服务及静态资源(负载)
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/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; upstream psccs {#这个名称可以自己写,但要对应上 server 192.168.0.1:8080 weight 2;#weight 权重根据需求配置 server 192.168.0.2:8080 weight 2;#server 是拥有的服务器IP+端口 } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} location / { root /var/www/static; #静态资源路径 } location /api { proxy_pass http://psccs/api/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } #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; # } #} }