服务器安装 Nginx

环境

Docker 还是直接安装 ?

Nginx 使用 Docker 安装属实有一些麻烦,各种前端文件还需要映射到容器内部,不是很推荐

正式安装

1、安装环境需要的依赖包

Bash
复制代码
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2、在你的linux服务器上创建目录:nginx

bash
复制代码
cd /root && mkdir nginx

3、进入刚才安装的目录

bash
复制代码
cd nginx

4、下载并且解压安装包

bash
复制代码
wget http://nginx.org/download/nginx-1.13.7.tar.gz && tar -zxvf nginx-1.13.7.tar.gz
image-20240816230851762

5、进入刚刚解压好的目录

text
复制代码
cd nginx-1.13.7

6、添加一些常用的模块,比如:gziphttps 模块

bash
复制代码
./configure --with-http_gzip_static_module --with-http_stub_status_module --with-http_stub_status_module --with-http_ssl_module
image-20240816231245265

PS 这个 nginx error log 不是报错,别看到红就紧张(像我一样🤣)

7、 执行下面的命令

bash
复制代码
make && make install
image-20240816231620756 image-20240816231650360

安装成功后的截图

8、设置 Linux 环境变量

1)编辑 /etc/profile 文件

任意目录下执行下面的命令

text
复制代码
vi /etc/profile

2)在最后一行添加下面的代码

bash
复制代码
PATH=$PATH:/usr/local/nginx/sbin export PATH

3)让配置生效

bash
复制代码
source /etc/profile

9、任意目录下输入

bash
复制代码
nginx

10、访问服务器 IP

image-20240816232616833

Nginx 常用命令

按照上面安装之后,nginx 的位置在哪里?

bash
复制代码
/usr/local/nginx/sbin

1、检查 Nginx 配置文件的语法

bash
复制代码
nginx -t
image-20240816233213758

2、显示 Nginx 版本信息

bash
复制代码
nginx -v
image-20240816233547479

3、查看编译时的配置参数 (大写的 V)

bash
复制代码
nginx -V
image-20240816233718185

4、重启 Nginx

bash
复制代码
nginx -s stop

5、重新加载配置文件

bash
复制代码
nginx -s reload
image-20240816233921342

如果出现上面的报错,就检查一下 nginx 配置文件

Nginx 配置文件

下面是默认的 Nginx 配置文件

nginx
复制代码
#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; } #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 -s reload 时报错 nginx: [error] invalid PID number

解决:

bash
复制代码
sudo pkill -f nginx

强制删除 Nginx 进程,然后再重新启动

bash
复制代码
nginx

相关专栏

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
leikooo
作者分享
Spring 团队开发者布道师 Josh Long,从 2011 年起每周二坚持写 This Week in Spring https://spring.io/authors/joshlong,大概 15 年半从未间断,到现在大概写了 800 期以上😱 大佬在采访里他说,写博客不是额外负担,而是逼自己整理每周所学的「强制机制」——反正本来就会刷社区动态,写出来既方便自己,也帮到别人。更重要的是 Spring 一直在变:微服务、AI……永远有新东西可聊,停一周就容易掉队。一旦养成习惯,坚持往往比重新开始更容易。 这种级别的大佬都还在用周更逼自己不掉队,我更没理由再拖了。还有之前左耳朵耗子大佬说的 ARTS 打卡,我老实说只撑了两周,真的需要捡起来了,加油✊
10
试了下 Grok CLI:curl -fsSL https://x.ai/cli/install.sh | bash 虽然功能不如 Claude Code 全,但能免费用 Grok 4.5 啊😍。一行 prompt 大概 3 分钟就生成出来了而且没有报错:" Three.js UMD 构建。正在实现完整的太阳系模拟(含自定义轨道控制,兼容本地打)"。 大伙可以访问试试:https://solar-system-seven-mocha.vercel.app/
4
彻底搞懂 Spring AI Tool Calling:从底层协议到源码执行全流程
7
别用 JWT 管理用户会话
9
没想到 Bot 占全球 HTML 流量的 50% 以上了,被这个比例给震惊到了。还有开发者在评论区说自己的网站「每天」访问量 250k 但是 Cloudflare 显示真实的用户只有 150 个😱 数据来源:https://radar.cloudflare.com/traffic#bot-vs-human
5
下载 APP