查看系统版本php
输入命令html
cat /etc/redhat-releaselinux
个人Centos版本nginx
CentOS Linux release 7.5.1804 (Core)c++
安装nginx所需的依赖正则表达式
gcc安装算法
安装 nginx 须要先将官网下载的源码进行编译,而编译依赖 gcc 环境,若是没有 gcc 环境,则须要安装:shell
yum install -y gcc-c++vim
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
zlib 安装
zlib 库提供了不少种压缩和解压缩的方式 nginx 使用 zlib 对 http 包的内容进行 gzip ,因此须要在 Centos 上安装 zlib 库
yum install -y zlib zlib-devel
OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不只支持 http 协议,还支持 https(即在ssl协议上传输http),因此须要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
下载地址:
选择稳定版本
使用xshell链接linux系统 再使用xftp上传文件到指定目录
个人目录
/app/tool/
解压到当前目录
tar zxf nginx-1.14.1.tar.gz
目录结构
解压完成后进入解压目录
cd /app/tool/nginx-1.14.1
配置
其实在 nginx-1.14.0 版本中你就不须要去配置相关东西,默认就能够了。固然,若是你要本身配置目录也是能够的。
默认配置(推荐)
编译
./configure
完成输出的结果
自定义配置
注:将临时文件目录指定为/var/temp/nginx,须要在/var下建立temp及nginx目录
./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
执行
make && make install
输出结果最后一行:
make[1]: Leaving directory `/app/tool/nginx-1.14.1'
查找安装路径
whereis nginx
输出结果:
nginx: /usr/local/nginx
进入sbin目录
cd /usr/local/nginx/sbin
启动
./nginx
先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s stop
中止:等待nginx进程处理任务完毕进行中止
./nginx -s quit
重启
先中止再启动
./nginx -s quit
./nginx
重载配置文件
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效须要重启 nginx,使用-s reload不用先中止 ngin x再启动 nginx 便可将配置信息在 nginx 中生效,以下:
./nginx -s reload
开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
编辑配置文件
修改/usr/local/nginx/conf下的 nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
修改配置文件
在nginx.conf的http{}里面添加
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
检查模块
进入/usr/local/nginx/sbin/目录下 查看nginx版本与编译安装了哪些模块
cd /usr/local/nginx/sbin/
./nginx -V
添加http_ssl_module模块
进入nginx解压目录下配置nginx
cd /app/tool/nginx-1.14.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译
千万不要make install 不然会覆盖现有的nginx
make
关闭nginx
进入/usr/local/nginx/sbin/目录下
./nginx -s quit
复制配置目录
将/app/tool/nginx-1.14.1/objs/nginx 替换/usr/local/nginx/sbin/nginx
复制前注意先将以前的nginx文件备份
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
复制 若是提示是否覆盖 键入y便可
cp /app/tool/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/
查看编译安装的模块
进入/usr/local/nginx/sbin/目录下
cd /usr/local/nginx/sbin/
./nginx -V
上传证书
我上传的目录为
/usr/local/nginx/conf
我使用的是腾讯云的免费证书
将 1_域名_bundle.crt
和 2_域名.key
上传到 /usr/local/nginx/conf/目录下
修改nginx.conf
vim /usr/local/nginx/conf/nginx.conf
在http{}
末尾添加如下内容
注:example.com应换成本身的域名
server { listen 80; server_name example.com; send_timeout 1800; rewrite ^(.*) https://example.com$1 permanent; } server { listen 443; server_name example.com; #填写绑定证书的域名 index index.html index.htm index.php default.html default.htm default.php; ssl on; ssl_certificate /usr/local/nginx/conf/1_example.com_bundle.crt; ssl_certificate_key /usr/local/nginx/conf/2_example.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置 ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }
验证配置是否正确
进入/usr/local/nginx/sbin/目录下
cd /usr/local/nginx/sbin/
./nginx -t
以下提示表示成功
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
开启443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
启动nginx
./nginx
如今就可使用https访问了
修改/usr/local/nginx/conf/nginx.conf
修改的位置为server{}里面,配置了https的须要在443端口server下添加以下内容,若是没有配置https则在默认80端口下添加便可vim conf/nginx.conf
内容
location /images/ { root /app/home/; autoindex on; }
修改图片目录的权限
个人目录为/app/home/
chmod 777 -R /app/home/
存放图片须要存入设置目录的images目录下
个人图片路径为
/app/home/images/
重载nginx配置文件
cd /usr/local/nginx/sbin/
./sbin -s reload
访问
如今就能够访问图片了,访问地址为
域名(iP:端口)/images/图片名称.后缀