Linux下搭建Nginx+MySQL+PHP.php
首先來介紹一下Nginx.Nginx是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。Nginx不只能夠做爲web服務器,也能夠做爲負載均衡器,以前也有文章介紹,你們能夠看一下.html
MySQL是一款開源免費的數據軟件,MySQL是一個小型關係型數據庫管理系統,其體積小、速度快、整體擁有成本低,尤爲是開放源碼這一特色,許多中小型網站爲了下降網站整體擁有成本而選擇了MySQL做爲網站數據庫.mysql
PHP,是英文超級文本預處理語言Hypertext Preprocessor的縮寫。PHP 是一種 HTML 內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言,語言的風格有相似於C語言,被普遍的運用。linux
下載最新版本1.8.0
nginx
http://nginx.org/en/download.html
web
nginx會有幾個依賴包,咱們首先安裝依賴,不要安裝過程當中會報錯:sql
yum -y install zlib-devel pcre-devel openssl-devel
通常源代碼安裝分4個步驟,解壓(tar命令)預編譯(執行源碼包下的configure),編譯(make),編譯安裝(make install)
首先咱們解壓源碼包:shell
tar -zxvf nginx-1.0.13.tar.gz
這裏解釋下加壓參數,z表明gzip(也就是後面的.gz文件)x表明加壓,v表示顯示詳細信息,-f使用檔案文件或設備(必選參數)數據庫
而後咱們進行預編譯,通常預編譯會帶上一些參數,已達到咱們想要安裝的效果,好比啓用某個功能,禁用某個功能:
進入源碼包目錄進行預編譯:瀏覽器
cd nginx-1.0.13 ./configure --prefix=/usr/local/nginx\ # 指定安裝目錄爲/usr/local/nginx --with-openssl=/usr/include/openssl\ # 啓用ssl --with-pcre\ # 啓用正規表達式 --with-http_stub_status_module # 安裝能夠查看nginx狀態的程序
其中./configure指執行當前目錄下的configure文件
預編譯完成後咱們就能夠進行編譯和安裝:
make #編譯
執行後make後會有大量輸出,等待輸出完成後若是沒有報錯就能夠進行安裝執行:
make install #安裝
安裝完成後咱們能夠到相應的目錄查看安裝的文件:
ls /usr/local/nginx/ conf html logs sbin
好了,下面咱們啓動nginx:
/usr/local/nginx/sbin/nginx
經過查看端口看nginx是否啓動成功,nginx佔用TCP的80端口,執行下面命令:
netstat -antlp ¦ grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5946/nginx
咱們查看80端口是開放的
而後打開瀏覽器訪問http://192.168.3.120,咱們會看到Welcome to nginx(以前的版本是 It’s Work):
nginx安裝完畢後咱們來安裝MySQL :
安裝依賴:
yum -y install ncurses-devel
建立MySQL用戶:
useradd -M -s /sbin/nologin mysql # -M不建立home目錄,-s指定shell爲不登陸
而後進行安裝:
tar -zxvf mysql-5.0.95.tar.gz cd mysql-5.0.95 ./configure --prefix=/usr/local/mysql \ --without-debug \ # 取消調試模式提升性能 --with-extra-charsets=utf8,gbk \ # 僅僅指定須要的默認字符集提升性能 --enable-assembler \ # 使用匯編模式提升性能 --with-mysqld-ldflags=-all-static \ # 以靜態方式編譯提升性能 --with-client-ldflags=-all-static \ --with-unix-socket-path=/tmp/mysql.sock \ # 使用unix socket提升性能 --with-ssl make make install
安裝完成後複製配置文件和啓動腳本:
cp support-files/my-medium.cnf /etc/my.cnf # 複製配置文件 cp support-files/mysql.server /etc/init.d/mysqld # 複製啓動腳本 chmod +x /etc/init.d/mysqld # 給啓動腳本執行權限
爲了之後方便咱們爲全部的二進制可執行文件和動態連接庫文件作一個軟鏈接:
ln -s /usr/local/mysql/bin/* /usr/local/bin/ # 爲可執行的二進制文件作軟鏈接 ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/ # 爲動態連接庫作一個軟鏈接
而後咱們初始化數據庫:
mysql_install_db --user=mysql # 用MySQL用戶安裝數據庫
爲了MySQL能正常使用咱們須要更改一下MySQL安裝目錄和MySQL的數據庫目錄的屬主和屬組:
chown -R root.mysql /usr/local/mysql/ # 更改安裝目錄屬主爲root,屬組爲mysql chown -R mysql.mysql /usr/local/mysql/var/ # 更改數據庫目錄屬主和屬組都爲mysql
這裏的-R參數用來應用到全部子目錄和文件
配置完畢後咱們啓動mysql:
service mysqld start
如今咱們查看MySQL是否啓動成功,MySQL佔用TCP的3306端口,咱們查看端口是否被佔用:
netstat -antlp ¦ grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 32143/mysqld
而後咱們經過mysql命令來鏈接mysql:
mysql
會顯示以下內容表示已經成功啓動MySQL並已經鏈接上
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.95-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
MySQL安裝完畢下面咱們就來安裝PHP,安裝PHP前首先要安裝幾個源碼包依賴:
libmcrypt mhash mcrypt
參考:http://www.th7.cn/system/lin/201410/74518.shtml
首先來安裝幾個源碼包依賴:
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2/download tar -jxvf libmcrypt-2.5.8.tar.bz2 # 這個包是bz2的 使用-j參數解壓 cd libmcrypt-2.5.8 ./configure make make install #################################################### wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download tar -jxvf mhash-0.9.9.9.tar.bz2 cd mhash-0.9.9.9 ./configure make make install # 這兩個包安裝完成後要把動態連接庫作一個軟鏈接到/usr/lib,覺得接下來的mcrypt依賴於這兩個包 ln -s /usr/local/lib/libmcrypt* /usr/lib ln -s /usr/local/lib/libmhash.* /usr/lib/ ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config ########################################################### wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download tar -zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 ./configure make make install
而後下載php:
wget http://cn2.php.net/get/php-5.4.0.tar.bz2/from/this/mirror
安裝依賴:
yum –y install libxml2-devel curl-devel libpng-devel openldap-devel
咱們使用nginx調用php的時候使用fpm的方式,在php 5.4中加入了對php-fpm的支持,因此就不須要打補丁了.安裝PHP:
tar -jxvf php-5.4.0.tar.bz2 cd php-5.4.0 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap make make install
到這裏整個LNMP已經安裝完成.下面咱們就配置php和nginx能運行php網站:
首先爲php建立配置文件:
cp php.ini-production /usr/local/php/php.ini # 若是是開發就複製php.ini-development cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ln -s /usr/local/php/bin/php /usr/bin/
配置php-fpm,編輯php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf
找到listen那一行,修改爲以下內容:
listen = /var/run/php-fpm/php-fpm.sock # 使用unix socket
啓動php-fpm
mkdir /var/run/php-fpm /usr/local/php/sbin/php-fpm
而後配置nginx,編輯nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
修改nginx配置文件支持php:
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.php index.html index.htm; # 添加index.php的首頁文件 } # 添加下面內容 location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; }
修改完畢後保存退出重啓nginx:
pkill -9 nignx /usr/local/nginx/sbin/nginx
而後在/usr/local/nginx/html下建立index.php,
vi /usr/local/nginx/html/index.php
添加下面內容:
[php] view plaincopy
<?php
phpinfo();
?>
保存退出後訪問http://192.168.3.120/index.php,看到下面頁面表示已經安裝配置成功: