Nginx安装

来源:互联网 发布:微信转换淘宝链接 编辑:程序博客网 时间:2024/06/05 06:50

Nginx安装环境

 Nginx是C语言开发,建议在linux上运行,本教程使用Centos7 x64安装环境。

1、gcc

 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++ 

2、PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

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

5、编译安装

下载Nginx,下载地址:http://nginx.org/download/ ,选择最新版本进行下载

wget http://nginx.org/download/nginx-1.13.0.tar.gz
解压:tar -zxvf nginx-1.13.0.tar.gz
cd nginx-1.13.0.tar.gz
./configure
make && make install

/usr/local/nginx/sbin/nginx -v 查看版本,检测是否安装成功/usr/local/nginx/sbin/nginx -v 查看版本,检测是否安装成功

6、Nginx配置

创建Nginx运行使用的用户www(名字自己起就行): groupadd www    ->   useradd -g www www
配置nginx.conf文件,修改成以下内容(在/usr/local/nginx/conf目录下):

user www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
http
{
  include mime.types;
  default_type application/octet-stream;
  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
 
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

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

7、配置Nginx来支持php

安装php7     
下载地址:https://secure.php.net/downloads.php
这里下载的是:wget http://ar2.php.net/distributions/php-7.0.6.tar.gz
下载之后解压并进入在解压文件中
安装:./configure  –enable-fpm (enable-fpm参数即可开启PHP-FPM)  ->  make && make install
(PHP在 5.3.3 之后已经讲php-fpm写入php源码核心了)

nginx整合php-fpm
1. 启动php-fpm: /usr/local/sbin/php-fpm
报错
[18-May-2016 18:07:58] ERROR: failed to open configuration file '/usr/local/etc/php-fpm.conf': No such file or directory
2.到/usr/local/etc/目录下,将php-fpm.conf.default拷贝一份成php-fpm.conf
root@iZ25fm7iewtZ:/# cd /usr/local/php/etc/
root@iZ25fm7iewtZ:/usr/local/etc# cp php-fpm.conf.default php-fpm.conf
3.然后在编辑php-fpm.conf配置文件
;最后一行改成如下
include=/usr/local/etc/php-fpm.d/*.conf
4.进入到/usr/local/etc/php-fpm.d/目录下,将www.conf.default拷贝一份成www.conf
root@iZ25fm7iewtZ:/usr/local/etc# cd php-fpm.d/
root@iZ25fm7iewtZ:/usr/local/etc/php-fpm.d# cp www.conf.default www.conf
编辑www.conf文件,将user和group改成和nginx.conf中的user和group一致
user = www
group = www
再次启动
/usr/local/sbin/php-fpm

root@iZ25fm7iewtZ:/usr/local/etc# ps -ef | grep php-fpm
root      3691     1  0 18:49 ?        00:00:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data  3692  3691  0 18:49 ?        00:00:00 php-fpm: pool www      
www-data  3693  3691  0 18:49 ?        00:00:00 php-fpm: pool www      
root      4982 29553  0 18:59 pts/1    00:00:00 grep --color=auto php-fpm
root@iZ25fm7iewtZ:/usr/local/etc# netstat -tnl | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN  

5.开机启动php-fpm,开机启动的配置文件是:/etc/rc.local ,加入 /usr/local/sbin/php-fpm 即可
vi /etc/rc.local   添加 /usr/local/sbin/php-fpm
6.修改nginx的配置文件,支持php文件的解析,找到location的添加位置,在后面添加下面这个location
location ~ \.php$ {
                        root html; #指定php的根目录
                        fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }

8、启动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文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(–conf-path= 指向配置文件(nginx.conf))

9、停止nginx

 方式1,快速停止:
 cd /usr/local/nginx/sbin
 ./nginx -s stop
 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

方式2,完整停止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。

10、重启nginx

  方式1,先停止再启动(建议使用):
  对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
  如下:
  ./nginx -s quit
  ./nginx

  方式2,重新加载配置文件:
  当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

  ./nginx -s reload

11、Centos 7防火墙firewalld开放端口

开启80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
出现success表明添加成功
命令含义:
--zone #作用域
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
--permanent   #永久生效,没有此参数重启后失效

重启防火墙

systemctl restart firewalld.service

运行、停止、禁用firewalld

启动:# systemctl start  firewalld
查看状态:# systemctl status firewalld 或者 firewall-cmd --state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld

原创粉丝点击