文章目录
一、nginx的安装
1、安装GCC、automake、pcre、zlib和openssl
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
2、下载nginx(不必追新,稳定版即可)
- wget工具的安装
yum -y install wget
- 下载nginx的压缩包,到 /etc目录下
wget -P /etc http://nginx.org/download/nginx-1.20.2.tar.gz
- 解压压缩包
tar -xvf nginx-1.20.2.tar.gz
3、安装nginx服务器、安装到 /etc/nginx 下
cd /etc
mkdir nginx
- 到之前解压的文件夹下
cd /etc/nginx-1.20.2
- 安装到指定目录 /etc/nginx
./configure --prefix=/etc/nginx
- make指令进行源代码编译
make
- 编译完成后,执行make install命令安装nginx服务器
make install
- 进入到安装的目录
cd /etc/nginx #至此安装结束
二、nginx的启动、重新启动、常用命令
-
本人的nginx安装在/etc/nginx
-
所以配置文件在/etc/nginx/conf
-
nagix安装成功默认端口为80
-
进入sbin
cd /etc/nginx/sbin
- 使用默认配置启动nginx
./nginx
- 查看naginx的进程号
ps -ef|grep nginx
- 重启nginx(修改配置文件后)
/etc/nginx/sbin/nginx -s reload
- 查看nginx的配置文件是否有语法错误
./nginx -t
- 查看nginx的版本
./nginx -v
其它
- 如果是访问错误,试试先关闭服务器的防火墙
1、在nginx开启代理,在一个端口上可以通过浏览器访问服务器上的资源
- 修改配置文件
#user nobody;
user root; #这个没写,访问可能会报403错误
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 /home/ljj/images; #端口资源的根路径设置为 /home/ljj/images
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服务器
- 即可以通过 ip:端口/a.jpg 访问到服务器上的/home/ljj/images/a.jpg资源,比如 165.24.152.240:80/a.jpg
- 重点