nginx是一个开源且高性能,可靠的http中间件,代理服务。
nginx下载:nginx.org
安装四个准备环境
1.gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
2.PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
3.zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
4.OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
下载
1.直接下载.tar.gz安装包
地址:https://nginx.org/en/download.html
下载使用命令:
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
2.解压
tar -zxvf nginx-1.16.1.tar.gz
进入解压后的目录:
cd nginx-1.16.1
3.配置
可使用自动配置
./configure
5.编译安装
make
make install
#查看安装路径
whereis nginx
启动停止nginx
#首先进入目录
cd /usr/local/nginx/sbin/
#启动
./nginx
#此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s stop
#此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s quit
#查询nginx进程
ps aux|grep nginx
#重新加载配置文件(此方法不用重新启动nginx)
./nginx -s reload
配置静态界面:
比如我把vue项目配置test目录下
cd /
mkdir test
#然后上传静态界面到目录中
#进入配置文件夹
cd /usr/local/nginx/conf/
#修改配置
vim 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 {
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;
# }
#}
include vhosts/*.conf;
}
#修改完成后保存,在此保存目录下新建一个目录
mkdir vhosts
#进入目录
cd vhosts
#执行编辑命令(会自动创建文件)
vim test.conf
#添加如下代码
server {
listen 80;
location /{
root /test;#指向nginx安装目录下的html文件夹
index index.html;
autoindex on;
}
}
#刷新配置即可