CentOS6.2/5.8下LNMP高性能的WEB服務器快速Yum搭建流程

本文將指導你如何在CentOS/Red Hat (RHEL) 6.2/5.8下使用Yum來搭建LEMP WEB服務器。國內

LEMP (Linux, Nginx, MySQL, PHP) 服務器目前在國內大的企業如百度騰訊使用非常普遍,但是因爲LEMP不易安裝配置,難爲了許多運維人員。在本安裝中,我儘量使用yum安裝而避免編譯安裝,有將有效減少安裝過程的時間及複雜程序。

LEMP(或LNMP)高性能的WEB服務器在CentOS6.2/5.8下的Yum搭建流程

STEP1.切換到root用戶

su -
## OR ##
sudo -i



STEP2.安裝必要的軟件源

 

1. 安裝Remi源

## Remi Dependency on CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
 
## CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
 
 
## Remi Dependency on CentOS 5 and Red Hat (RHEL) 5 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
 
## CentOS 5 and Red Hat (RHEL) 5 ## 
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm


2.配置Nginx 源

創建/etc/yum.repos.d/nginx.repo文件並寫入以下內容

CentOS

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1


RedHat(RHEL)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1



STEP3.安裝Nginx

yum --enablerepo=remi,remi-test install nginx


STEP4.安裝PHP5.4.4&PHP-FPM

yum --enablerepo=remi,remi-test install php php-fpm php-common

STEP5.安裝PHP5.4.4模塊擴展(一些擴展可能無用,請自行去除)

yum --enablerepo=remi,remi-test install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml


STEP6.停止並禁用httpd服務,啓動Nginx HTTP服務及PHP-FPM

1.  停止httpd

/etc/init.d/httpd stop
## OR ##
service httpd stop
chkconfig httpd off

2.  啓動Nginx

/etc/init.d/nginx start
## OR ##
service nginx start

3.  啓動PHP_FPM

/etc/init.d/php-fpm start
## OR ##
service php-fpm start

4. 配置nginx使用PHP-FPM,修改/etc/nginx/conf.d/default.conf 

(1) 先爲/etc/nginx/conf.d/default.conf作一個備份

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf_bak

(2) 在default.conf 文件中,找到以下內容。

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

修改爲:

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
}
nginx默認的wwwroot文件夾爲/usr/share/nginx/html,這裏你可以修改爲自己指定的目錄
(3) 去除以下內容前的#號,並修改fastcgi_param所在行

#location ~ \.php$ {
#       root           html;
#       fastcgi_pass   127.0.0.1:9000;
#       fastcgi_index  index.php;
#       fastcgi_param  SCRIPT_FILENAME  [將此處修改爲wwwroot路徑]$fastcgi_script_name;
#        include        fastcgi_params;
#}

修改結果

location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
}


STEP7.配置iptables防火牆開啓80端口

爲Nginx Web Server開放80端口,修改/etc/sysconfig/iptables文件,加入如下內容

cd /etc/nginx/sites-enabled/
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重啓iptables防火牆

cd /etc/nginx/sites-enabled/
service iptables restart
## OR ##
/etc/init.d/iptables restart



STEP8.測試Nginx及PHP-FPM

1. 重啓nginx及php-fpm

service nginx restart
service php-fpm restart


2. 打開瀏覽器,訪問http://localhost或http://服務器ip,如果出現以下頁面,說明nginx安裝成功

3. 在/usr/share/nginx/html文件夾下創建phpinfo.php文件,內容如下

<?php   
    phpinfo();  
?> 
訪問http://localhost/phpinfo.php或http://服務器ip/phpinfo.php,如果出現以下頁面,說明PHP-FPM配置安裝成功



STEP9. 安裝mysql5.5

1. 安裝mysql5.5

yum --enablerepo=remi,remi-test install mysql mysql-server

2. 啓動mysql並配置mysql自啓動

/etc/init.d/mysqld start 
## OR ##
service mysqld start 
chkconfig --levels 235 mysqld on

3. 進行mysql 初始安全設置

  • 設置(修改)root密碼
  • 刪除匿名用戶
  • 禁用root遠程登錄
  • 刪除測試數據庫test
  • 重載權限表
要啓用MySQL 安全設置請輸入以下命令
/usr/bin/mysql_secure_installation

輸出
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
 
In order to log into MySQL to secure it, we\'ll need the current
password for the root user.  If you\'ve just installed MySQL, and
you haven\'t set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] Y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] Y
 ... Success!
 
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] Y
 ... Success!
 
Cleaning up...
 
 
 
All done!  If you\'ve completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!



【注】:如果您不想啓動MySQL 安全設置命令,但至少也得修改一下root用戶密碼

mysqladmin -u root password [your_password_here]
## 示例##
mysqladmin -u root password myownsecrectpass

4. 配置防火牆開啓3306端口
修改/etc/sysconfig/iptables 文件:
vi /etc/sysconfig/iptables
2. 在COMMIT之前加入以下內容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
3. 重啓Iptables:
service iptables restart
## OR ##
/etc/init.d/iptables restart

至此我們的LEMP(或LNMP)高性能的WEB服務器搭建完成

更多資料可參考

1. Nginx&PHP-FPM安裝請參考
2. Mysql5.5安裝請參考

********************************************
* 作者:葉文濤 
* 本文鏈接:LEMP(或LNMP)高性能的WEB服務器在CentOS6.2/5.8下的Yum搭建流程
******************轉載請註明來源 ***************

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章