服务器安装Nginx
环境
- CentOS 8
- 使用编译安装 Nginx
Nginx使用Docker安装确实有点麻烦,需要将很多前端文件映射到容器内部,不推荐
正式环境
1、安装环境需要的依赖包
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、在你的linux服务器上创建目录:nginx
cd /root && mkdir nginx
3、进入刚才安装的目录
cd nginx
4、下载并且解压安装包
wget http://nginx.org/download/nginx-1.13.7.tar.gz && tar -zxvf nginx-1.13.7.tar.gz
5、进入刚刚解压好的目录
cd nginx-1.13.7
6、添加一些常用的模块,比如:gzip、https 模块
./configure --with-http_gzip_static_module --with-http_stub_status_module --with-http_stub_status_module --with-http_ssl_module
7、 执行下面的命令
make && make install
make指令报错解决办法:https://blog.csdn.net/qq_45697992/article/details/120028370
8、设置 Linux 环境变量
1)编辑 /etc/profile 文件
vi /etc/profile
2)在最后一行添加下面的代码
PATH=$PATH:/usr/local/nginx/sbin
export PATH
3)让配置生效
source /etc/profile
9、任意目录下输入
nginx
10、访问服务器 IP
curl http://localhost
Nginx 常用命令
按照上面安装之后,nginx 的位置在哪里?
/usr/local/nginx/sbin
1、检查 Nginx 配置文件的语法
nginx -t
2、显示 Nginx 版本信息
nginx -v
3、查看编译时的配置参数 (大写的 V)
nginx -V
4、重启 Nginx
nginx -s stop
5、重新加载配置文件
nginx -s reload
如果出现上面的报错,就检查一下 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
解决:
sudo pkill -f nginx
强制删除 Nginx 进程,然后再重新启动
nginx
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析