设为首页 收藏本站
查看: 1185|回复: 0

[经验分享] CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx1.6)

[复制链接]

尚未签到

发表于 2018-4-24 10:50:01 | 显示全部楼层 |阅读模式
  环境:
  系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡)
  系统版本:CentOS-7.0-1406-x86_64-DVD.iso
  安装步骤:
  1.准备
  1.1 显示系统版本
[root@centos ~]# cat /etc/redhat-release


  CentOS Linux>
[root@centos ~]# uname -a

  Linux tCentos7 3.10.0-123.13.1.el7.x86_64 #1 SMP Tue Dec 9 23:06:09 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  1.2 安装基本软件包
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y

[root@centos ~]# yum install net-tools bind-utils -y

  1.3 显示IP地址 (centos7需要先安装 net-tools bind-utils包)
[root@centos ~]# ifconfig|grep inet

  inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
  2.编译安装nginx
  2.1 下载包
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

[root@centos ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz

[root@centos ~]# wget http://zlib.net/zlib-1.2.8.tar.gz

[root@centos ~]# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz

[root@centos ~]# wget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2

  2.2 安装依赖
[root@centos ~]# yum install zlib-devel openssl-devel -y

  2.3 关闭selinux (不关有时会添加不了用户,或者重启后没法开机)
[root@centos ~]#  vim /etc/selinux/config

  屏蔽以下两行
  #SELINUX=enforcing
  #SELINUXTYPE=targeted
  添加以下一行
  SELINUXTYPE=disabled
  保存,退出
  重启后,查询是否关闭(显示Disabled则表示关闭)
[root@centos ~]#  shutdown -r now

[root@centos ~]#  getenforce

  Disabled
  2.4 安装Pcre
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf pcre-8.36.tar.gz

[root@centos ~]# cd pcre-8.36

[root@centos ~]# ./configure

[root@centos ~]# make && make install

  2.5 安装openssl
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf openssl-1.0.1j.tar.gz

[root@centos ~]# cd openssl-1.0.1j

[root@centos ~]# ./config

[root@centos ~]# make && make install

  2.6 安装zlib
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf zlib-1.2.8.tar.gz

[root@centos ~]# cd zlib-1.2.8

[root@centos ~]# ./configure

[root@centos ~]# make && make install

  2.7 安装jemalloc
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar xjf jemalloc-3.6.0.tar.bz2

[root@centos ~]# cd jemalloc-3.6.0

[root@centos ~]# ./configure

[root@centos ~]# make && make install

[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf

[root@centos ~]# ldconfig

  2.3 创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限
[root@centos ~]# groupadd www

[root@centos ~]# useradd -g www www -s /sbin/nologin

[root@centos ~]# mkdir -p /data/www

[root@centos ~]# chmod +w /data/www

[root@centos ~]# chown -R www:www /data/www

  ***如果没法创建用户,需要检查SELinux状态是否关闭
  2.8 安装nginx
[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf nginx-1.6.2.tar.gz

[root@centos ~]# cd nginx-1.6.2

[root@centos ~]# ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36 --with-ld-opt="-ljemalloc"

[root@centos ~]# make && make install

  2.9 修改 nginx.conf
[root@centos ~]# vim /opt/nginx/conf/nginx.conf

  修改前面几行为:
  user www www;
  worker_processes auto;
  error_log logs/error.log crit;
  pid logs/nginx.pid;
  events{
  use epoll;
  worker_connections 65535;
  }
  找到,并修改 root 行的内容
  location / {
  root /data/www;
  index index.html index.htm;
  }
  保存,退出
  2.10 建立测试首页
[root@centos ~]# vim /data/www/index.html

  <html>
  <head><title>nginx index.html</title></head>
  <body>
  <h1>index.html</h1>
  </body>
  </html>
  保存,退出
  2.11 测试和运行
[root@centos ~]# cd /opt/nginx

[root@centos ~]# ldconfig

[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf -t

  如果显示下面信息,即表示配置没问题
  nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
  查看jemalloc是否生效,需要先启动nginx
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf

[root@centos ~]# lsof -n | grep jemalloc

  ginx 2346 root mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
  nginx 2347 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
  nginx 2348 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
  nginx 2349 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
  nginx 2350 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
  2.12 防火墙添加80端口
[root@centos ~]# iptables -L|grep ACCEPT

[root@centos ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

[root@centos ~]# firewall-cmd --reload

[root@centos ~]# iptables -L|grep ACCEPT

  2.13 浏览器打开
  http://192.168.1.10
  显示出欢迎内容,则表示成功
  2.14 作为服务,开机后启动
[root@centos ~]# vim /usr/lib/systemd/system/nginx.service

  增加以下内容
[Unit]

  Description=The nginx HTTP and reverse proxy server
  After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]

  Type=forking
  PIDFile=/opt/nginx/logs/nginx.pid
  ExecStartPre=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -t
  ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
  ExecReload=/bin/kill -s HUP $MAINPID
  ExecStop=/bin/kill -s QUIT $MAINPID
  PrivateTmp=true
[Install]

  WantedBy=multi-user.target
  :wq 保存退出
[root@centos ~]# systemctl enable nginx.service

[root@centos ~]# systemctl list-unit-files|grep enabled|grep nginx

  2.15 启动服务
[root@centos ~]# ./sbin/nginx -s stop

[root@centos ~]# systemctl daemon-reload

[root@centos ~]# systemctl start nginx.service

[root@centos ~]# systemctl status nginx.service -l

[root@centos ~]# ps -ef|grep nginx

[root@centos ~]# lsof -n | grep jemalloc

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451298-1-1.html 上篇帖子: CentOS7 minimal版 安装gnome桌面 下篇帖子: yum& ip centos6
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表