1.安装依赖,如果已经安装了依赖,则跳过
yum -y install gcc gcc-c++ wget net-tools pcre-devel zlib-devel openssl-devel
2.下载并解压安装包
#进入常用文件夹
cd /usr/local/src/#下载源码
wget http://nginx.org/download/nginx-1.7.12.tar.gz#解压
tar zxvf nginx-1.7.12.tar.gz#进入目录
cd nginx-1.7.12
3.安装
./configure --prefix=/usr/local/nginx
在这部会出现一些错误,一些错误解决方法如下:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
安装pcre-devel解决问题 : yum -y install pcre-devel
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib
library statically from the source with nginx by using –with-zlib= option.
则需要安装“zlib-devel”即可 : yum install -y zlib-devel
4.继续安装
#编辑
make
#查看是否有错误
echo $? //显示0则成功 其他数组则失败
#安装
make install
5.启动
# 查看 Nginx 版本
nginx -v
# 查看 Nginx 安装目录
rpm -ql nginx
# 启动 Nginx
service nginx start
# 重启 Nginx
service nginx restart
6.配置项目
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# root /usr/share/nginx/html/web;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#配置静态图片访问地址
location /images { //图片访问前缀
alias /usr/share/nginx/html/server/images/; //图片存放路径
autoindex on;
}
location / {
root /usr/share/nginx/html/web; //项目存放路径
index index.html;
}
当项目为单页面时,浏览器刷新后报404,下面是解决办法:
location / {
root /mydata/transfer/html/helper/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
7.查看项目
注意:可能在访问图像的时候会宝禁止访问错误‘forbid’问题,这时候要对图像的存放文件夹修改访问权限
#进入图像存放文件夹
cd xx/xx/xx/
# 修改访问权限,加入-R 参数,就可以将读写权限传递给子文件夹,讲解权限操作的博客文章:https://blog.csdn.net/u013197629/article/details/73608613可以参考下。
chmod -R 777 /usr/share/nginx/html/server/images