Linux-Nginx安装及配置文件设置
1. 下载初始依赖
yum install gcc # 下载c++ 编译需要使用
yum -y install pcre-devel
yum -y install openssl openssl-devel
2. 下载压缩包
nginx百度网盘下载:https://pan.baidu.com/s/1aklBC1odfwMYELF9ur1FPQ
提取码:6666
3. 上传服务器并解压
tar zxvf 压缩包地址 -C 指定位置 # 解压后,指定位置目录下会出现一个nginx-1.20.2的目录
4. 编译
# 进入1.20.2
cd 解压目录/nginx-1.20.2
./configure
make
make install
5. 运行
cd ../ # 返回到上级目录
cd sbin # 指定的解压目录下有个sbin,进入
./nginx # 启动
6. 测试
打开浏览器,输入:服务器ip,正常情况下会出现欢迎使用nginx的页面
7. 相关命令
# 在sbin目录下
# 停止使用nginx
./nginx -s stop
# 修改配置文件后重新下载
./nginx -s reload
# 配置文件的位置
指定的解压目录(比如/usr/local/nginx)/conf/nginx.conf
8. 搭配域名使用
如果要使用自己的域名的话,要保证域名进行了备案,时间有点小长
配置文件举例如下:
#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;
# blog可以换成自己定义的
upstream blog{
server localhost:端口号;
}
server {
listen 80;
server_name 域名;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://blog;
}
#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就OK了