标签 nginx 下的文章

本文章将展示如何安装一个提供 ISPConfig 3 的基于 CentOS 7 的服务器,ISPConfig 3是一个网页服务器控制面板工具,通过它,你可以通过网页浏览器管理以下这些软件:nginx 网页服务器、postfix 邮件服务器、mysql 数据库服务器、BIND 域名解析服务器、pureFTPd 文件服务器、SpamAssassinClamAVMailman等,从 ISPConfig 3 开始,其已经可以完美支持Nginx服务器了。

第一步:下载 CentOS 7 安装镜像

你可以通过下面这两个链接地址下载该镜像:

我你爱我吗的是最小安装版,然后按自己的需要安装。

第二步:安装说明

我是在我的 MacbookPro 上面跑Parallels Desktop 虚拟机安装的 CentOS 7,其IP地址为:10.211.55.6,使用域名 centos7.localserver.com 域作为主机名。

第三步:安装编辑器编辑 /etc/hosts

若你喜欢使用 nano 编辑器,使用以下命令安装:

yum -y install nano

安装 wget 工具:

yum -y install wget

我使用的是 vi ,所以就不安装其它的编辑器了,在以下的所有步骤里面,我也将全部都使用 vi 编辑文件。

设定 /etc/hosts

vi /etc/hosts

内容如下:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
10.211.55.6 centos7.localserver.com
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

设置 hostnamecentos7.localserver.com

echo 'centos7.localserver.com' > /etc/hostname

第四步:安装并配置一些基本的网络防火墙管理软件

若您只想使用CentOS默认的防火墙软件,则可以直接跳过此步。

关闭并禁用 CentOS 默认的 Firewall 软件:

systemctl stop firewalld.service
systemctl disable firewalld.service

运行结果如下:

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
rm '/etc/systemd/system/basic.target.wants/firewalld.service'

使用以下命令查看当前的 Firewall 状态:

firewall-cmd --state

运行结果如下:

[root@localhost ~]# firewall-cmd --state
not running

接着安装新的软件:

yum -y install net-tools NetworkManager-tui

第五步:关闭 SELinux

Selinux 是CentOS的一个安装性扩展,我个人感觉一般都用不着该扩展,启用他,还会给我们系统的使用和管理带来很多麻烦的事儿,所以,我一般都直接关闭该扩展。关闭该扩展只需要将 /etc/selinux/config 中的 SELINUX 项设置为 disabled 即可:

vi /etc/selinux/config

作如下设定:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

保存修改之后,还必须重启一次系统:

reboot

最近我的一个项目刚刚上线(日PV > 500W),流量突然之间很大,但是由于我们前期仅仅只是想小范围测试,所以没有准备足够的服务器资源,导致整个应用的访问均变得很慢,最主要的是我们的应用需要从服务器端向多个第三方发起多次请求,而每一次请求都会需要很长的时间,所以,整个应用几乎就到了不能使用的地步了。

排查原因之后,我们的服务器完全没有问题,而问题就是出在了当我们向第三方发起请求之后,第三方需要5分钟左右的时间才能给出结果,这个时候我们前端没有做任何的过滤,这使得有了12406效应,就是我们不出结果,导致前端的用户就一直的试这么一个恶性循坏,临时性的想到的一个办法就是我在我们的应用服务器前端再加一台服务器,若在短时间内,请求次数超过某个阈值,我就直接拒绝请求,使用了 Nginxngx_http_limit_req_module 模块。

/etc/nginx/nginx.conf 文件中,加入如下代码:

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=20r/s;

    ...
}

上面这一行的作用是:

  • 定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,
  • 以$binary_remote_addr 为key,限制平均每秒的请求为20个,
  • 1M能存储16000个状态,rete的值必须为整数,
  • 如果限制两秒钟一个请求,可以设置成30r/m

然后在我们的 server 中添加如下代码:

server {
    limit_req zone=one burst=5;
    ...
}

作用为:

  • 限制每ip每秒不超过20个请求,漏桶数burst为5
  • brust的意思就是,如果第1秒、2,3,4秒请求为19个,
  • 第5秒的请求为25个是被允许的。
  • 但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。
  • nodelay,如果不设置该选项,严格使用平均速率限制请求数,
  • 第1秒25个请求时,5个请求放到第2秒执行,
  • 设置nodelay,25个请求将在第1秒执行。

我们假设系统目录结构如下:

/path/to/app/
    controllers/
    models/
    views/
    public/
        css/
        js/
        img/
        index.php

index.php 为入口文件。

Apache

.htaccess 文件存放在 /path/to/app/public/ 目录下,同时编辑该文件,内容如下:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

一个完整的虚拟主机配置文件示例:

<VirtualHost *:80>

    ServerAdmin admin@example.host
    DocumentRoot "/path/to/app/public"
    DirectoryIndex index.php
    ServerName example.host
    ServerAlias www.example.host

    <Directory "/path/to/app/public">
        Options All
        AllowOverride All
        Allow from all
    </Directory>

</VirtualHost>

Nginx

index index.php index.html index.htm;
try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass php;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
    root $app_root/public;
}

location ~ /\.ht {
    deny all;
}

Nginx、MySQL 与 PHP 可以联合使用成为一个强大的动态内容服务器,这三者在不同的系统上面有不同的名称,本文所要讲解的是如何在 FreeBSD 上面搭建该环境,我们称之为 FEMP。我所使用的 FreeBSD 版本为 10.1。

第一步:安装这些软件

使用下面的命令即可完成软件的安装:

sudo pkg install nginx mysql56-server php56 php56-mysql

在安装完成之后,若您使用的是系统默认的 tcsh ,我们需要立即运行 rehash 命令。

添加第三方软件源

PHP-FPM 在 CentOS 的官方程序库中不存在,所以我们首先得添加第三方扩展软件源。

rpm --import https://fedoraproject.org/static/0608B895.txt 
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum install yum-priorities

然后编辑:vi /etc/yum.repos.d/epel.repo

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://mirrors.aliyun.com/epel/6/$basearch
        http://mirrors.aliyuncs.com/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
priority=10
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

接着:vi /etc/yum.repos.d/remi.repo

[remi]
name=Les RPM de remi pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror
enabled=1
priority=10
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

安装 Nginx、MySQL以及PHP

#安装MySQL服务器
yum install mysql mysql-server

#安装Nginx
yum install nginx

#安装PHP及相关组件
yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy php-pecl-apc

待所有安装结束后,使用以下命令启动服务:

#启动MySQL
chkconfig --levels 235 mysqld on
service mysqld start

#启动Nginx
chkconfig --levels 235 nginx on
service nginx start

#启动PHP-FPM
chkconfig --levels 235 php-fpm on
service php-fpm start

若 Nginx 启动失败,则有可能是因为 Apache httpd 服务占用了该接口,这时,要么我们修改 Nginx 的坚挺端口,要么修改Apache httpd的或者直接删除 Apache httpd。

apachectl stop
yum remove httpd
chkconfig --level 235 httpd off

配置Nginx、MySQL与PHP-FPM

运行 mysql_secure_installation 命令,提升 MySQL 服务器的安全性,要求您要输入数据库 root 帐户密码,若您是新安装,则直接回车即可,接着立马设置新密码。

配置 PHP

打开 PHP 配置文件:vi /etc/php.ini

设置 cgi.fix_pathinfo=0

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

设置 upload_max_filesize 最大文件上传尺寸为 upload_max_filesize16MB

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 16M

设置 post_max_size32MB

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 32M

打开:vi /etc/php-fpm.d/www.conf

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock;

以及:

listen.owner = www
listen.group = www
listen.mode = 0660

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = www
; RPM: Keep a group allowed to write in log dir.
group = www

最近越来越发现自己变懒了,没有以前那么强的追根问底的勇气了,最近这几个月腾讯云一直在做活动,三折起,我买了两台服务器,配置也还不错,最高的配置的那一台现在是做为公司的备用服务器在使用,而还有一个配置稍低一些的,则做自己的个人使用了,对于个人这一台,我最开始是安装的SUSE作为服务器操作系统,然后,发现软件版本的各种老啊,如果是以前的话,可能我会通宵达旦地把这软件给升级咯,可是现在却选择了一个更简单的方法,把SUSE换成了CentOS,然后同样的问题出现了,然后,我又回到了Ubuntu的怀抱。

从各种网站上面是各种搜索,只是为了想知道,到底CentOS、Suse与Ubuntu哪一个发行版做服务器更好,最后的结果是没有人能告诉我一个确切的答案,公说公有理,婆说婆有理,索性,咱回到业务的出发点,不就是为了要一台服务器么,只要能实现我的需求,那就行,然后在各种比较之后,我还是选择了最易用的Ubuntu 12.04 LTS,然后,再一次开始这个环境的安装与配置。

安装 Nginx、MySQL、PHP

如果想运行最新版本的Nginx(版本一般会高于Ubuntu官方的),需要先运行下面两行命令:

aptitude install python-software-properties
add-apt-repository ppa:nginx/stable

直接运行下面整段命令即可完成所有必须软件的安装,对于PHP,我还安装了一些功能库:

apt-get update
apt-get upgrade
apt-get install nginx mysql-client mysql-server memcached php5-common php5-dev php5-cgi php5-fpm php-apc php5-mysql php5-pgsql php5-sqlite php5-curl php5-gd php5-intl php-pear php5-mcrypt php5-memcache php5-ming php5-recode php5-tidy php5-xmlrpc php5-xsl php5-mcrypt php5-imap
注意:安装过程中,需要输入 MySQL 服务器 root 用户的密码。

安装完成之后,各服务就会自动启动了,直接访问服务器的IP即可访问,各配置文件分别保存在:

  • /etc/php5/fpm/php.ini
  • /etc/nginx/

这些目录下。

建议

  1. 将PHP-FPM 默认监听的 127.0.0.1:9000 改成了 /var/run/php5-fpm.sock,改了之后,需要把 nginx.conf 添加以下代码片段:

    upstream php {

      server unix:/var/run/php5-fpm.sock;

    }

  2. 我的 nginx 虚拟机路径都被设置为 /home/www/PRIMARY_DOMAIN_NAME/public ,需要把 PRIMARY_DOMAIN_NAME 修改成为虚拟主机主域名。

今天由智者科技运营的智者教学平台正式上线了,使用的是开源的在线学习系统——Moodle,我们的服务器使用的是 Nginx ,Moodle 的配置文件如下:

server {
    listen 80;
    server_name school.ofsz.com;
    autoindex on;
    root /srv/sites/school.ofsz.com/public;
    error_log /srv/logs/school.ofsz.com.error.log;
    access_log /srv/logs/school.ofsz.com.access.log;
    index index.php index.html index.htm;
    location ~ \.php {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_param  PATH_INFO      $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
     
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;
     
        fastcgi_param  SCRIPT_NAME    $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  REQUEST_URI    $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
     
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
     
        fastcgi_param  REMOTE_ADDR    $remote_addr;
        fastcgi_param  REMOTE_PORT    $remote_port;
        fastcgi_param  SERVER_ADDR    $server_addr;
        fastcgi_param  SERVER_PORT    $server_port;
        fastcgi_param  SERVER_NAME    $server_name;
     
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
    }


    location / {
        try_files $uri $uri/ /index.html;
    }

}