Linux下安装Nginx服务器

来源:互联网 发布:淘宝聚便宜 编辑:程序博客网 时间:2024/05/19 03:28

安装Nginx之前,首先要安装好编译环境gcc和g++,然后以CentOS为例安装Nginx,安装Nginx需要PRCE库、zlib库和ssl的支持,除了ssl外其他的我们都是去官网下载:

  Nginx:http://nginx.org/

  PCRE:http://www.pcre.org/

  zlib:http://www.zlib.net/

  首先将包准备好,上传至服务器下,开始安装

  首先释放pcre,并不用安装:

tar -xvzf pcre-8.38.tar.gz

  然后释放zlib:

tar -xvzf zlib-1.2.8.tar.gz

  安装openssl:

yum -y install openssl openssl-devel

  释放nginx:

tar -xvzf nginx-1.9.8.tar.gz

  然后执行 cd nginx-1.9.8 进入目录开始编译安装nginx:

这两个选择一个

 ./configure --user=nginx --group=nginx --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.10
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8
makemake install

  注意../pcre-8.38和../zlib-1.28是刚刚释放的pcre和zlib的源码目录,编译时nginx会编译到一起

  稍微等待编译安装就完成了,安装位置就是--prefix指定的/usr/local/nginx,配置文件位置:/usr/local/nginx/nginx.conf

  然后确定80端口没有被占用的情况下,启动nginx服务:

/usr/local/nginx/nginx

  启动成功后,使用浏览器访问对应的IP,就可以看到初始页面了,

  

启动nginx导致无法访问解决如下

[emerg]: getpwnam(“nginx”) failed


1
2
[root@localhost nginx-1.11.2]# /usr/local/nginx/sbin/nginx
nginx: [emerg] getpwnam("nginx") failed

 

没有安装nginx用户导致的无法启动

1
2
[root@localhost nginx-1.11.2]# useradd -s /sbin/nologin -M nginx
[root@localhost nginx-1.11.2]# id nginx

 

 

1
2
3
[root@localhost nginx-1.11.2]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.11.2]# netstat -tlunp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9709/nginx: master 

  



  到这里nginx服务器就安装成功了

  除了刚才的启动命令外,一些常用的nginx命令如下:

  检查nginx.conf配置文件的正确性: /usr/local/nginx/nginx -t 

  重启nginx: /usr/local/nginx/nginx -s reopen 

  停止nginx: /usr/local/nginx/nginx -s stop 

  重新载入配置文件: /usr/local/nginx/nginx -s reload 



原创粉丝点击