在CentOS 6.2上安裝Nginx+PHP5(PHP-FPM)+MySQL

1.使用非官方軟件源 php

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

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm html

yum install yum-priorities
編輯/etc/yum.repos.d/epel.repo文件,啓用這個源,使其優先級最高 mysql

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

一樣的,也須要對remi源進行優先級編輯和啓用 nginx

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

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

2.安裝MYSQL 數據庫

yum install mysql mysql-server

設置系統啓動時,在235的運行級運行mysqld守護進程 瀏覽器

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

須要按系統配置優化my.cnf文件,以後設置root的密碼,不然任何人都能訪問你的數據庫。 bash

[root@server1 ~]# 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): < -- ENTER 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] <-- ENTER New password: <-- yourrootsqlpassword Re-enter new password: <-- yourrootsqlpassword 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] <-- ENTER ... 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] <-- ENTER ... 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] <-- ENTER - 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] <-- ENTER ... 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!  
  [root@server1 ~]#

3.安裝nginx 服務器

nginx在epel源中有包,能夠直接yum安裝。 php-fpm

yum install nginx

以後建立nginx的系統啓動鏈接並啓動它

chkconfig --levels 235 nginx on
service nginx start

以後在瀏覽器中打開服務器的IP地址,應該能看到nginx的歡迎頁面。

4.安裝php和php-fpm

yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

其中使用的php-x擴展,能夠根據本身的須要添加。以後,編輯php.ini文件,修改cgi.fix_pathinfo爲0。爲何要這麼作?請查閱該文檔

vi /etc/php.ini
cgi.fix_pathinfo = 0 date.timezone = "Asia/Shanghai"

5.配置nginx

關於配置nginx能夠查閱官方的兩個例子,例子1例子2

6.讓PHP-FPM使用unix套接字

默認狀況下,php-fpm使用127.0.0.1:9000監聽請求,固然也能夠使用unix套接字,這樣能夠避免TCP的額外流量。編輯/etc/php-fpm.d/www.conf文件就能夠了。

vi /etc/php-fpm.d/www.conf
[...] ;listen = 127.0.0.1:9000 listen = /tmp/php5-fpm.sock [...]

以後,重啓php-fpm。

service php-fpm restart

以後編輯nginx的虛擬主機配置文件,將fastcgi_pass這一行內容改成unix套接字。

[...] location ~ \.php$ { try_files $uri =404;
            root /usr/share/nginx/html;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params; } [...]

最後,重啓nginx。

service nginx reload
相關文章
相關標籤/搜索