基于CentO S7搭建nginx服务器

一、1.建立两个文件夹,用于储存nginx以及一些模块
mkdir /usr/local/src/tarbag
mkdir /usr/local/src/software
在这里插入图片描述

2.进入tarbag文件夹中

cd /usr/local/src/tarbag/
在这里插入图片描述
3.下载nginx,
wget http://www.nginx.org/download/nginx-1.0.6.tar.gz
在这里插入图片描述
如果提示没有wget命令则需要下载yum -y install wget
4.下载模块:
wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
在这里插入图片描述

二、编译安装

1.将压缩包解压到移动到/usr/local/src/software/路径中然后解压tar -zxvf nginx-1.0.6.tar.gz

2.解压之后编译安装:先进入解压之后的文件夹内:cd nginx-1.0.6
然后开始编译安装:
./configure --prefix=/usr/local/nginx-1.0.6 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=…/ngx_cache_purge-1.3
在这里插入图片描述
输入之后就开始安装了。
然后再输入
make
make install
就可以完成安装了
3.之后优化一下内核参数
vi sysctl.conf 增加以下配置(中文删掉)
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 1800
net.ipv4.ip_conntrack_max = 16777216 # 如果使用默认参数,容易出现网络丢包
net.ipv4.netfilter.ip_conntrack_max = 16777216# 如果使用默认参数,容易出现网络丢包
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65535
保存退出后:使配置生效
Sysctl -p

三、
1.修改nginx配置文件
进入目录cd /usr/local/src/software/nginx-1.0.6/conf/
然后修改配置文件vim nginx.conf,修改成如下配置:
在这里插入图片描述

在这里插入图片描述

注意:如果有多个域名就按上述要求往下修改。
2.创建测试也文件夹: mkdir /var/www/html/www.gyq28.com
创建之后进入该目录,创建测试页:touch index.html
然后添加内容(随意)vim index.html
添加之后保存退出
3.在终端输入/usr/local/nginx-1.0.6/sbin/nginx -t //看到 ok 和 successful,说明配置文件没问题

在这里插入图片描述

/usr/local/nginx-1.0.6/sbin/nginx -s reload //重载 nginx
注意:如果在重新加载的过程中出现这种问题:nginx: [error] open() “/usr/local/nginx-1.0.6/logs/nginx.pid” failed (2: No such file or directory)则需要运行下列语句:
使用nginx -c的参数指定nginx.conf文件的位置

在这里插入图片描述
/usr/local/nginx-1.0.6/sbin/nginx -s stop //关闭 nginx

4.启动nging命令: /usr/local/nginx-1.0.6/sbin/nginx
如果启动的时候出现端口被占用如下:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
则需要关闭apache,
关闭systemctl stop httpd
四、绑定hosts,测试
Vim /etc/hosts
添加:192.168.1.105 www.gyq28.com(设置身IP)
保存退出。
修改window下的host文件:C:\Windows\System32\drivers\etc\hosts
添加192.168.1.105 www.gyq28.com(设置身IP)
保存退出。
五、验证
在这里插入图片描述
成功可以使用nginx访问网站

注意:使用域名访问需要配置dns(我的博客里有相关配置),访问之前需要关闭防火墙:systemctl stop firewalld 然后再关闭selinux:setenforce 0(临时关闭,关机重启后会还原)。

发布了11 篇原创文章 · 获赞 3 · 访问量 694

猜你喜欢

转载自blog.csdn.net/gyqailxj/article/details/100749649