在Fedora 14中安裝Nginx+MySQL+PHP5(PHP-FPM模式) LNMP

來源:互聯網
上載者:User
文章目錄
  • 1 前言備忘
  • 2.安裝MySQL5
  • 3安裝 Nginx
在Fedora 14中安裝Nginx+MySQL+PHP5(PHP-FPM模式) LNMP

2010-12-25 20:17:40|  分類:Fedora|  標籤:php  nginx  mysql  root  html  |字型大小大中小 訂閱

Nginx(發音為 “engine x”)是一款免費、開源、高效能的HTTP伺服器。同時Nginx以穩定、功能豐富、配置簡單、資源消耗少著稱。這篇教程將會為你展示如何在一台 Fedora 14中安裝Nginx+MySQL+PHP5(PHP-FPM模式)

我已經測試無誤,這將保證為你工作!

1 前言備忘

在這篇教程中我使用的使用者名稱是www.unixbar.net,IP地址是192.168.0.100.這些設定可能與你的有所不同,因此 你需要在適當的地方修改一下.

2.安裝MySQL5

我們通過執行下面的命令來安裝MySQL:

yum install mysql mysql-server

然後我們為MySQL建立系統啟動串連(這樣的話,MySQL就會在系統啟動的時候自動啟動)並且啟動MySQL伺服器:

chkconfig --levels235 mysqld on/etc/init.d/mysqld start

現在檢查是否支援網路訪問,運行:

netstat-tap|grep mysql

應該顯示如下資訊:

[root@server1 ~]# netstat -tap | grep mysql tcp        0      0 *:mysql                     *:*                         LISTEN      1717/mysqld [root@server1 ~]#

如果不顯示,編輯/etc/my.cnf檔案,並注釋掉skip-networking參數:

vi/etc/my.cnf
[...] #skip-networking [...]

並重啟 MySQL 伺服器:

/etc/init.d/mysqld restart

為root使用者佈建一個密碼(否則任何人都可以訪問到你的MySQL資料庫):

[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQLSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the currentpassword for the root user.  If you've just installed MySQL, andyou 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 MySQLroot user without the proper authorisation. Set root password? [Y/n]<--敲 ENTER鍵 New password: <-- 輸入你的密碼Re-enter new password: <-- 重複輸入你的密碼Password updated successfully!Reloading privilege tables..... Success! By default, a MySQL installation has an anonymous user, allowing anyoneto log into MySQL without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment. Remove anonymous users? [Y/n] <--敲 ENTER鍵 ... Success! Normally, root should only be allowed to connect from 'localhost'.  Thisensures 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 canaccess.  This is also intended only for testing, and should be removedbefore 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 farwill 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 MySQLinstallation should now be secure. Thanks for using MySQL! [root@server1 ~]#
3安裝 Nginx

Nginx是Fedora 14的預設包,我們可以通過下列命令安裝它:

yum install nginx

然後我們為nginx建立一個系統啟動連結,並啟動它:

chkconfig --levels235 nginx on /etc/init.d/nginx start

在你的瀏覽器中輸入你的伺服器IP地址或者主機名稱(例如http://192.168.0.100),然後你就可以看到nginx的歡迎頁面:

4 安裝PHP5
Fedora 14官方的源中,並沒有php-fpm,但是 我們可以使Remi repository源。使用下列命令添加Remi repository源

rpm -ivh http://rpms.famillecollet.com/remi-release-14.rpm

下面開啟tc/yum.repos.d/remi.repo檔案.

vi/etc/yum.repos.d/remi.repo

把[remi]部門的enabled改為1。Gpgcheck改為0.其他不需要修改

[remi]name=Les RPM de remi pour Fedora $releasever - $basearch#baseurl=http://rpms.famillecollet.com/fedora/$releasever/remi/$basearch/mirrorlist=http://rpms.famillecollet.com/fedora/$releasever/remi/mirrorenabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remifailovermethod=priority [remi-test]name=Les RPM de remi en test pour Fedora $releasever - $basearch#baseurl=http://rpms.famillecollet.com/fedora/$releasever/test/$basearch/mirrorlist=http://rpms.famillecollet.com/fedora/$releasever/test/mirrorenabled=0gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remifailovermethod=priority

現在我們安裝php-cli和其他的PHP5模 塊,例如php-mysql,它可以使你的PHP指令碼支援MySQL:

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

然後開啟/etc/php.ini檔案,並解除登出cgi.fix_pathinfo=1這一行:

vi/etc/php.ini
[...]; 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-pathinfocgi.fix_pathinfo=1[...]

現在我們建立php-fpm的系統啟動串連,並啟動它:

chkconfig --levels235 php-fpm on/etc/init.d/php-fpm start

Php-fpm是一個後台進程(初始化指令碼為/etc/init.d/php-fpm),以9000連接埠運行在一台FastCGI伺服器上.

5 配置nginx
現在我們開啟nginx的設定檔/etc/nginx/nginx.conf:

vi/etc/nginx/nginx.conf

設定檔簡單易懂
(你可以在下列網站學習更多的配置方法http://wiki.codemongers.com/NginxFullExample 和http://wiki.codemongers.com/NginxFullExample2)
首先你可以增加worker process的數量和設定keepalive_timeout為一個合理值:

[...]worker_processes  5;[...]keepalive_timeout  2;[...]

虛擬機器主機定義在server{}容器中.我們使用下列命令修改預設的虛擬機器主機:

[...]    server {        listen       80;        server_name  _;         #charset koi8-r;         #access_log  logs/host.access.log  main;         location / {            root   /usr/share/nginx/html;            index  index.php index.html index.htm;        }         error_page  404              /404.html;        location = /404.html {            root   /usr/share/nginx/html;        }         # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   /usr/share/nginx/html;        }         # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        location ~ \.php$ {            root           /usr/share/nginx/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;        }         # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        location ~ /\.ht {            deny  all;        }    }[...]

server_name  ; 你可以在這裡通過修改www.unixbar.net來確 定你的網域名稱
在location /部分,我在index行加入了index.php。root /usr/share/nginx/html 意思是文檔路徑為/usr/share/nginx/html。
對於PHP來說最重要的部分就是 location ~ \.php$ {}。取消它的注釋。改變root這一行為網站的文檔路徑。例如root /usr/share/nginx/html。請確保把fastcgi-param行修改成了fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;;否則的話PHP解析器將不會找到瀏覽器中調用的PHP.
現在我們儲存檔案並重啟nginx:

/etc/init.d/nginx restart

現在在文檔路徑root /usr/share/nginx/html建立下列PHP檔案:

vi/usr/share/nginx/html/info.php
<?phpphpinfo();?>

現在我們就可以在瀏覽器中通過http://192.168.0.100/info.php訪 問了。

正如你在Server API這一行中所看到的一樣,PHP5現在已經以FPM/FastCGI模式正常運行了。如果你繼續向下翻看,你就能過看到PHP5所支援的模組,其中就包括 MySQL模組:

 

7 原文地址

http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-fedora-14

http://www.unixbar.net/linux/fedora/1106.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.