一、安装准备(二选一执行就好)
首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装
$ yum install gcc-c++
$ yum install pcre pcre-devel
$ yum install zlib zlib-devel
$ yum install openssl openssl--devel
合并成一条命令
$ yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、安装 PCRE(PCRE 作用是让 Nginx 支持 Rewrite 功能)
1、下载 PCRE 安装包(文件下载的目录/root)
$ cd /root
$ wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解压安装包
$ tar zxvf pcre-8.35.tar.gz
$ mv pcre-8.35 /usr/local/
3、进入安装包目录
$ cd /usr/local/pcre-8.35
4、编译安装
$ ./configure
$ make && make install
5、查看pcre版本
$ pcre-config --version
三、安装Nginx
安装之前,最好检查一下是否已经安装有nginx
$ find -name nginx
如果系统已经安装了nginx,那么就先卸载
$ yum remove nginx
首先进入/rootl目录(这个只是下载nginx到本机的目录)
$ cd /root
从官网下载最新版的nginx
$ wget http://nginx.org/download/nginx-1.9.5.tar.gz
解压nginx压缩包
$ tar -zxvf nginx-1.9.5.tar.gz
$ mv nginx-1.9.5 /usr/local/
会产生一个nginx-1.9.5 目录,这时进入nginx-1.9.5目录
$ cd /usr/local/nginx-1.9.5
接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35
$ make
$ make install
如果没有报错,顺利完成后,最好看一下nginx的安装目录
$ whereis nginx
安装完毕后,进入安装后目录(/usr/local/nginx)便可以启动或停止它了。
安装完毕后,进入 cd /usr/local/nginx/sbin/ 目录,启动nginx:
./nginx
到此,使用CentOS安装nginx已经完成了,其实看看还是蛮简单的。
配置 HTTPS
在阿里云申请免费一年的CA证书
1.登录阿里云
2.搜索SSL证书(或着在产品-->安全-->SSL证书)
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 btmcheck.com;
return 301 https://$server_name$request_uri;
}
# HTTPS server
server {
listen 443 ssl;
server_name btmcheck.com;
ssl_certificate /home/ca/2197624_btmcheck.com.key;
ssl_certificate_key /home/ca/2197624_btmcheck.com.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# admin
location /admin {
proxy_pass http://localhost:8089/;
proxy_redirect off;
default_type application/json;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
client_max_body_size 25m;
}
# web
location /web/ {
proxy_pass http://localhost:8088/;
proxy_redirect off;
default_type application/json;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
client_max_body_size 25m;
}
# 图片配置
location /file-server {
add_header 'Access-Control-Allow-Origin' '*';
alias /home/project/xxx/pic/;
}
}
}
重启nginx (我的在/usr/local/nginx/sbin目录下)
cd /usr/local/nginx/sbin
./nginx -s reload
报错[emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:69解决
1.安装OpenSSL
yum -y install openssl openssl-devel
2.在nginx安装目录下执行
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
3.运行命令
make
4.然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
5.关闭nginx
./nginx -s stop
6.将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
cp ./objs/nginx /usr/local/nginx/sbin/
7.通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V