009 Linux下编译安装Nginx服务器 并 修改Nginx服务器访问根目录,009nginx
009 Linux下编译安装Nginx服务器 并 修改Nginx服务器访问根目录,009nginx
一、 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++
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命令下载1.10.1版本(推荐)。
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
三、安装
1、解压
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
2、配置
其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置
./configure
输出:
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、编译安装
make
make install
查找安装路径:"/usr/local/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端口。
五、更改nginx访问根目录
安装完nginx服务器后发现nginx的根目录在 安装目录的/html/下(/usr/local/nginx/html/),但是对于部署文件来说,在该目录下是不太习惯的,我就尝试着更改nginx访问的根目录
1、更改nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
更改如下:
listen 80;
server_name localhost;
location / {
root /home/ftpuser/wwwRoot; #新的根目录
index index.html index.htm index.jpg; #添加一张图片,测试用。
}
2、nginx根目录权限设置:
chmod -R 755 /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
/usr/local/nginx/sbin/nginx
注意:执行 /usr/local/nginx/sbin/nginx,这里可以-c指定加载的nginx配置文件,如下:
/usr/local/nginx/sbin/nginx -c /xxxxx
如果不指定-c,nginx在启动时默认加载/usr/local/nginx/conf/nginx.conf文件。
2 停止nginx
/usr/local/nginx/sbin/nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
3 重启nginx:先停止再启动
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx
版权声明:本文为博主原创文章,未经博主允许不得转载。
评论暂时关闭