百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> centos安装nginx、mysql、php的方法(lnmp)
分享文章到:

centos安装nginx、mysql、php的方法(lnmp)

发布时间:01/15 来源: 浏览: 关键词:
前面介绍过ubuntu安装lamp环境下面我们看一个centos最快捷简单安装nginx、mysql、php的解决方案,希望文章给各位同学有所帮助。

对于lnmp的安装,每个运维人员可能都进行过上百次。这里提供一个我认为最简单高效的安装方案。

1.升级yum到最新版本:

 代码如下

yum -y update

2.用yum对lnmp服务器依赖的组件进行安装:

 代码如下

LANG=C

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

3.用yum安装php、mysql客户端和服务端、php-fastcgi和常用的php组件:

 代码如下

yum -y install php mysql mysql-server mysql-devel php-mysql php-cgi php-mbstring php-gd php-fastcgi php-fpm

4.用yum安装nginx,但yum默认源没有nginx,需要更改为nginx官方地址:

 代码如下

wget wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

5.用yum安装nginx:

 代码如下

yum install nginx

6.设置nginx自动启动:

 代码如下

chkconfig nginx on

chkconfig php-fpm on

chkconfig mysqld on

7.用以下命令分别启动三个服务:

 代码如下

nginx

php-fpm &

service mysqld start

 

以上所有安装完毕。最后打开nginx配置文件,设置php-fpm,在nginx配置文件中server内加入以下内容(注意更改fastcgi_param的路径为自己的路径):

 代码如下

location ~ .php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /opt/www$fastcgi_script_name;

include fastcgi_params;

}

安装完成,创建phpinfo查看。

整个过程在网络良好的情况下花费5分钟以内。网络不好的同学请更新yum源到网易的源,速度很快。


后了后面我们再看一些nginx安全配置

做过虚拟主机的都知道,Apache有一个很好用的地方---配置php_admin_value,在里面配置一下open_basedir就可以了,但是Nginx却没有这样的设置,但是没有设置这项,一旦某用户上传了一个phpspy之类的东西,其他用户数据就遭殃了,今天就来解决这样的问题,怎么样让用户无法旁注。

首先,需要在php.ini 设置open_basedir的值,比如网站目录全部在 /data/web/ 下面,例如 /data/web/xxx.com/ 下面是xxx.com网站 那么我可以设置 open_basedir="/data/web/:/tmp/" (注意,/tmp 必须设置,否则影响上传文件,如果你设置了 upload_tmp_dir ,那么把/tmp设置成 upload_tmp_dir 的值)

然后执行

 代码如下

# chmod 755 -R /data/web/
# chmod 711 /data/web/

这时,网站依然能访问,可是你试试上传一个phpspy,看看还能列 /data/web/的目录么?

但是问题又来了,如果说/data/web/下目录命名非常有规律,很容易被猜解到,直接输入 /data/web/xxx.com 就可以列别人的目录,别急,还有办法,把 /data/web/xxx.com 重命名为复杂的名字,比如 /data/web/xxx.com_2a8b4c76  再把display_errors 关闭

现在我们再配置防跨站

测试环境为lnmp一键安装包环境。请根据自己的环境替换相关命令。

 代码如下


tar zxvf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1 //打完fpm补丁后再修改php源程序
cd php-5.2.17/
wget -c http://soft.vpser.net/web/php/bug/php-5.2.17-max-input-vars.patch //hash dos漏洞补丁
patch -p1 < php-5.2.17-max-input-vars.patch
./buildconf --force
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir --w
ith-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sy
svsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --enable-ftp --with-gd
--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic


vi main/fopen_wrappers.c
找到

 代码如下


/* {{{ php_check_open_basedir
*/
PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
{
        /* Only check when open_basedir is available */
        if (PG(open_basedir) && *PG(open_basedir)) {
                char *pathbuf;
                char *ptr;
                char *end;
                // add by anxsoft.com
                char *env_doc_root;
                if(PG(doc_root)){
                        env_doc_root = estrdup(PG(doc_root));
                }else{
                        env_doc_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
                }
                if(env_doc_root){
                        int        res_root = php_check_specific_open_basedir(env_doc_root, path TSRMLS_CC);
                        efree(env_doc_root);
                        if (res_root == 0) {
                                return 0;
                        }
                        if (res_root == -2) {
                                errno = EPERM;
                                return -1;
                        }
                }
                // add by anxsoft.com

 


                pathbuf = estrdup(PG(open_basedir));


                ptr = pathbuf;


                while (ptr && *ptr) {
                        end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
                        if (end != NULL) {
                                *end = '';
                                end++;
                        }


                        if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
                                efree(pathbuf);
                                return 0;
                        }


                        ptr = end;
                }
                if (warn) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));
                }
                efree(pathbuf);
                errno = EPERM; /* we deny permission to open it www.111cn.net */
                return -1;
        }


        /* Nothing to check... */
        return 0;
}
/* }}} */


红字是修改加上去的

保存退出后再编译安装

make ZEND_EXTRA_LIBS='-liconv'
make install


最后不要忘了将php.ini中的open_basedir改为:open_basedir = "/var/tmp/:/tmp/"


 删除不需要的Nginx模块

  我们可能根据我们的需要配置Nginx,当然在编译时可以选择某些不需要的模块不编译进去,比如精简掉autoindex和SSI模块,命令如下:

  ./configure --without-http_autoindex_module --without-http_ssi_module

  make

  make install

  当然在编译前可以通过下面的命令查看那些模块是可以开启或者关闭的:

  ./configure --help | less

修改Nginx服务器名称和版本号

  著名的NETCRAFT网站可以很轻松的查到你服务器的操作系统和服务程序版本,或者HTTP Response Header也能向我们透露这些信息,很多情况下,这些信息将为黑客进行攻击提供依据,因此我们需要对其进行伪装。

  编译Nginx源文件src/http/ngx_http_header_filter_module.c,输入以下命令:

  vi +48 src/http/ngx_http_header_filter_module.c

  找到下面两行:

 代码如下

  static char ngx_http_server_string[] = "Server: nginx" CRLF;

  static char ngx_http_server_full_string[] = "Server: "NGINX_VER CRLF;

  改成如下,当然具体显示什么你可以自己定义:

 代码如下

  static char ngx_http_server_string[] = "Server: NOYB" CRLF;

  static char ngx_http_server_full_string[] = "Server: NOYB" CRLF;

 修改Nginx配置文件

  3.1 避免缓冲区溢出攻击

  修改nginx.conf并且为所有客户端设置缓冲区大小限制:

  vi /usr/local/nginx/conf/nginx.conf

  编辑并且设置如下:

 代码如下

  ## Start: Size Limits &Buffer Overflows ##

  client_body_buffer_size 1K;

  client_header_buffer_size 1k;

  client_max_body_size 1k;

  large_client_header_buffers 2 1k;

  ## END: Size Limits &Buffer Overflows ##

  当然也许你还需要配置下面的内容以便于改善服务器性能:

 代码如下

  ## Start: Timeouts ##

  client_body_timeout 10;

  client_header_timeout 10;

  keepalive_timeout 5 5;

  send_timeout 10;

  ## End: Timeouts ##

  3.2 限制一些访问

  仅允许访问我们指定的域名,避免有人扫描绑定当前IP的所有域名,或者避免直接的IP访问以及恶意的域名绑定:

 代码如下

  ## Only requests to our Host are allowed

  ## i.e. nixcraft.in, images.nixcraft.in and www.nixcraft.in

  if ($host !~ ^(nixcraft.in|www.nixcraft.in|images.nixcraft.in)$ ) {

  return 444;

  }

  ##

  当然,网上还流传这么个写法:

 代码如下

  server {

  listen 80 default;

  server_name _;

  return 500;

  }

  限制一些方法,一般GET和POST已经够我们用了,其实HTTP还定义有类似于DELETE、SEARCH等方法,用不到的话就拒绝这些方法访问服务器:

 代码如下

  ## Only allow these request methods ##

  if ($request_method !~ ^(GET|HEAD|POST)$ ) {

  return 444;

  }

  ## Do not accept DELETE, SEARCH and other methods ##

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有2人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板