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

來源:互聯網
上載者:User

對於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 ##

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.