Centos7安装、卸载nginx及配置,配置成系统服务方式(一步到位)
前言
最近斥巨资买了台服务器,现记录下nginx安装配置过程。
一、下载安装解压
1.进入临时文件夹里(随便一个都行)
cd /tmp/
2.下载并安装nginx压缩包
wget http://nginx.org/download/nginx-1.23.3.tar.gz
3.解压该压缩包
tar -xvf nginx-1.23.3.tar.gz
4.创建目标文件夹
cd /tmp/nginx-1.23.3
5.(默认会安装在/usr/local/nginx)这里通过configure命令指定安装目录
./configure --prefix=/data/nginx
6.编译安装
make && make install
7.最后生成的文件夹具体如下

二、SSL模块安装(SSL证书)用于htpps请求 没此需求可略过
./configure --prefix=/data/nginx --with-http_ssl_module
三、运行
1.进入nginx下的sbin目录
cd /data/nginx/sbin
2.执行启动
./nginx
3.查看nginx是否启动
ps -ef | grep nginx

4.浏览器访问你的IP(如下就是成功了)

四、卸载
1.查看nginx是否运行
ps aux | grep nginx
2.进入nginx下的sbin目录
cd /data/nginx/sbin
3.停止nginx运行
./nginx -s stop
4.查看与Nginx有关的文件夹
find / -name nginx
5.删除与Nginx有关的文件
rm -rf file /data/nginx*
6.再查看
find / -name nginx*
7.卸载Nginx的依赖
yum remove nginx
五、Nginx的基本操作命令
1.进入nginx下的sbin目录
cd /data/nginx/sbin
2.启动
./nginx
3.关闭
./nginx -s stop
4.重启
./nginx -s reload
六、配置成系统服务
1.创建nginx.service文件
vim /usr/lib/systemd/system/nginx.service
2.nginx.service文件中写入内容
[Unit] Description=nginx web service Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking PIDFile=/data/nginx/logs/nginx.pid ExecStartPre=/data/nginx/sbin/nginx -t -c /data/nginx/conf/nginx.conf ExecStart=/data/nginx/sbin/nginx ExecReload=/data/nginx/sbin/nginx -s reload ExecStop=/data/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=default.target
3.改权限
chmod 755 /usr/lib/systemd/system/nginx.service
4.文件生效
systemctl daemon-reload
5.设置开机自启
systemctl enable nginx.service
七、系统服务操作Nginx基本命令
1.启动
systemctl start nginx
2.停止
systemctl stop nginx
3.重启
systemctl restart nginx
4.重新加载配置文件
systemctl reload nginx
5. 查看Nginx状态
systemctl status nginx
6.开机自动
systemctl enable nginx
八、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服务器,利用它的反向代理功能提供负载均衡支持
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 8585;#监听端口
server_name localhost;#主机ip
#请求转发
location / {
proxy_pass http://localhost:8001;
}
location /app{
try_files $uri $uri/ /app/index.html;
}
#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;
# }
#}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Nginx配置为HTTPS的完整步骤(支持 SSL/TLS 加密)
Nginx是一个高性能的HTTP和反向代理web服务器,是运维中十分常用的中间件,HTTPS协议简单来说就是HTTP协议和SSL/TLS协议的组合,这篇文章主要介绍了Nginx配置为HTTPS(支持 SSL/TLS 加密)的相关资料,需要的朋友可以参考下2025-11-11
浏览器控制台报错Failed to load module script:解决方
这篇文章主要为大家介绍了浏览器控制台报错Failed to load module script:解决方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-11-11
Nginx location和proxy_pass配置示例详解
这篇文章主要介绍了Nginx location和proxy_pass配置的相关资料,本文详细探讨了Nginx配置中`location`和`proxy_pass`指令的不同组合方式及其对请求转发路径的影响,通过列举多种组合,展示了`location`匹配目录与`proxy_pass`地址路径如何相互作用,需要的朋友可以参考下2024-11-11


最新评论