Linux-011 centos7搭建Nginx服务器 并 修改Nginx服务器访问根目录
来源:互联网 发布:网络引论 编辑:程序博客网 时间:2024/06/04 18:10
一、 nginx安装环境
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
该命令等效于下面4句:
1、 gcc 安装
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++
2、 PCRE pcre-devel 安装PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
3、 zlib 安装
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
4、OpenSSL 安装
OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
二、下载nginx
1.直接下载.tar.gz
安装包,地址:https://nginx.org/en/download.html
2.使用wget
命令下载(推荐)。
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
三、安装
1、解压
依然是直接命令:
tar -zxvf nginx-1.10.1.tar.gzcd nginx-1.10.1
2、配置
其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。1.使用默认配置
./configure
2.自定义配置(不推荐)
./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
注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
执行:./configure ,结果:
[root@localhost nginx-1.12.2]# ./configure
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
.......
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
3、编译安装
makemake install
查找安装路径:
whereis nginx
四、开放80端口、开放http服务,重启防火墙
- firewall-cmd --zone=public --add-port=80/tcp --permanent
- firewall-cmd --permanent --zone=public --add-service=http
- firewall-cmd --reload
- firewall-cmd --list-all #查看开放服务、端口中是否有http服务和80端口。
[root@localhost nginx-1.12.2]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: dhcpv6-client ssh ftp http #已开放ssh、ftp、http服务
ports: 21/tcp 80/tcp #已开放21、80端口
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
五、更改nginx访问根目录
安装完nginx服务器后发现nginx的根目录在 安装目录的/html/下(/usr/local/nginx/html/),但是对于部署文件来说,在该目录下是不太习惯的,我就尝试着更改nginx访问的根目录
1、更改nginx配置文件 vi /usr/local/nginx/conf/nginx.conf
1、更改nginx配置文件 vi /usr/local/nginx/conf/nginx.conf
# :wq! #保存并退出
2、nginx根目录权限设置:
为了方便学习,我通常这样设置权限,命令:
chmod -R 755 /home/ftpuser/wwwRoot/
如果为保证安全,可这样设置:
1)设置新的访问根目录权限为所有人可执行(不然就没权限访问根目录了,一访问就是403错误!)
chmod 711 /home/ftpuser/wwwRoot/
2) 设置目录下文件为所有人可读权限(不然就没法读文件了,一读就是403错误!)
chmod -R 744 /home/ftpuser/wwwRoot/*
3、重启nginx务器
service nginx restart
4、测试
拷贝一直张jpg格式图片到:/home/ftpuser/wwwRoot/目录下,改名为:index.jpg 。
chmod 744 /home/ftpuser/wwwRoot/index.jpg #设置所有人可读。
访问http://localhost/,图片即在网页中显示出来。
设置完成后此时访问http://localhost/XXX 即为/home/www/XXX
六、使用nginx
1 启动nginx
ln -s /usr/local/nginx/sbin/nginx /bin/nginx
从此以后直接在命令行输入:
nginx
或
/usr/local/nginx/sbin/nginx
或
cd /usr/local/nginx/sbin/
./nginx
查询nginx进程:
ps aux|grep nginx
注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件。
2 停止nginx
方式1,快速停止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
方式2,完整停止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
3 重启nginx
方式1,先停止再启动(建议使用):
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:
./nginx -s quit
./nginx
方式2,重新加载配置文件启动
(更改配置后使用)
:
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
./nginx -s reload
- Linux-011 centos7搭建Nginx服务器 并 修改Nginx服务器访问根目录
- linux(centos7)搭建nginx服务器
- nginx服务器配置ftp服务器图片访问根目录
- CentOS7安装并配置nginx服务器
- centos7搭建nginx+多tomcat服务器环境
- Linux上搭建Nginx服务器
- Linux下搭建nginx服务器
- nginx服务器更改网站根目录
- Centos7.3_64位服务器下搭建Nginx+Tomcat多域名多站点访问
- linux-搭建nginx服务器--搭建php
- Nginx服务器访问异常
- nginx服务器访问优化
- 服务器(1)--搭建Nginx服务器
- Nginx搭建HTTPS服务器
- nginx搭建https服务器
- nginx搭建rtmp服务器
- nginx resin服务器搭建
- nginx搭建rtmp服务器
- 关于html中使用table表格tr不换行的问题
- 自己动手写RecyclerView的下拉刷新
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
- 会说话
- Qt自定义控件之QSwitchButton
- Linux-011 centos7搭建Nginx服务器 并 修改Nginx服务器访问根目录
- Android-Studio-Gradle
- selenium webdriverapi
- java web 面试准备-Spring
- SQL与eclipse的连接,从数据库读取表数据,将二维数组数据导入表
- ubuntu下安装MP4Box
- oracle_mybatis批量修改数据
- Linux磁盘扩容
- 均分纸牌-vijos