lnmp环境搭建完全手册(四)——lnmp搭建(源码安装)
首先來介紹一下Nginx.Nginx是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。Nginx不僅可以作為web服務器,也可以作為負載均衡器,之前也有文章介紹,大家可以看一下.
MySQL是一款開源免費的數(shù)據(jù)軟件,MySQL是一個小型關(guān)系型數(shù)據(jù)庫管理系統(tǒng),其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網(wǎng)站為了降低網(wǎng)站總體擁有成本而選擇了MySQL作為網(wǎng)站數(shù)據(jù)庫.
PHP,是英文超級文本預處理語言Hypertext Preprocessor的縮寫。PHP 是一種 HTML 內(nèi)嵌式的語言,是一種在服務器端執(zhí)行的嵌入HTML文檔的腳本語言,語言的風格有類似于C語言,被廣泛的運用。
nginx當前最新穩(wěn)定版是nginx-1.0.13
首先我們下載nginx,在Linux下執(zhí)行下面命令:
nginx會有幾個依賴包,我們首先安裝依賴,不要安裝過程中會報錯:
yum -y install zlib-devel pcre-devel openssl-devel 一般源代碼安裝分4個步驟(有人也會省去第一個步驟),解壓(tar命令)預編譯(執(zhí)行源碼包下的configure),編譯(make),編譯安裝(make install)
首先我們解壓源碼包:
這里解釋下加壓參數(shù),z代表gzip(也就是后面的.gz文件)x代表加壓,v表示顯示詳細信息,-f使用檔案文件或設(shè)備(必選參數(shù))
然后我們進行預編譯,一般預編譯會帶上一些參數(shù),已達到我們想要安裝的效果,比如啟用某個功能,禁用某個功能:
進入源碼包目錄進行預編譯:
其中./configure指執(zhí)行當前目錄下的configure文件
預編譯完成后我們就可以進行編譯和安裝:
make #編譯執(zhí)行后make后會有大量輸出,等待輸出完成后如果沒有報錯就可以進行安裝執(zhí)行:
make install #安裝安裝完成后我們可以到相應的目錄查看安裝的文件:
ls /usr/local/nginx/ conf html logs sbin好了,下面我們啟動nginx:
/usr/local/nginx/sbin/nginx通過查看端口看nginx是否啟動成功,nginx占用TCP的80端口,執(zhí)行下面命令:
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 ,我們使用MySQl-5.0.95版首先下載:
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.95.tar.gz <span style="font-family: Arial;">安裝之前我們先做一些準備工作,</span>安裝依賴:
yum -y install ncurses-devel創(chuàng)建MySQL用戶:
useradd -M -s /sbin/nologin mysql # -M不創(chuàng)建home目錄,-s指定shell為不登錄然后進行安裝:
tar -zxvf mysql-5.0.95.tar.gz cd mysql-5.0.95 ./configure --prefix=/usr/local/mysql \ --without-debug \ # 取消調(diào)試模式提高性能 --with-extra-charsets=utf8,gbk \ # 僅僅指定需要的默認字符集提高性能 --enable-assembler \ # 使用匯編模式提高性能 --with-mysqld-ldflags=-all-static \ # 以靜態(tài)方式編譯提高性能 --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 # 給啟動腳本執(zhí)行權(quán)限為了以后方便我們?yōu)樗械亩M制可執(zhí)行文件和動態(tài)鏈接庫文件做一個軟連接:
ln -s /usr/local/mysql/bin/* /usr/local/bin/ # 為可執(zhí)行的二進制文件做軟連接 ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/ # 為動態(tài)鏈接庫做一個軟連接然后我們初始化數(shù)據(jù)庫:
mysql_install_db --user=mysql # 用MySQL用戶安裝數(shù)據(jù)庫為了MySQL能正常使用我們需要更改一下MySQL安裝目錄和MySQL的數(shù)據(jù)庫目錄的屬主和屬組:
chown -R root.mysql /usr/local/mysql/ # 更改安裝目錄屬主為root,屬組為mysql chown -R mysql.mysql /usr/local/mysql/var/ # 更改數(shù)據(jù)庫目錄屬主和屬組都為mysql這里的-R參數(shù)用來應用到所有子目錄和文件
配置完畢后我們啟動mysql:
service mysqld start現(xiàn)在我們查看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會顯示如下內(nèi)容表示已經(jīng)成功啟動MySQL并已經(jīng)連接上
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.95-log Source distributionCopyright (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
首先來安裝幾個源碼包依賴:
安裝依賴:
yum install <span style="font-family: Arial, Helvetica, sans-serif;">–y</span> libxml2-devel curl-devel libpng-devel openldap-devel libxslt-devel freetype-devel </pre><pre code_snippet_id="1910093" snippet_file_name="blog_20161001_24_4125831" class="other" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar -zxvf libconv-1.1.14.tar.gz .configure --prefix=/usr/local/libiconv 安裝libmcrypt庫 </pre><pre> wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum install libmcrypt-devel -y yum install mhash -y我們使用nginx調(diào)用php的時候使用fpm的方式,在php 5.4中加入了對php-fpm的支持,所以就不需要打補丁了.安裝PHP:
tar -jxvf php-5.3.28.tar.bz2 cd php-5.3.28 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp --enable-opcache=no make make install </pre><pre code_snippet_id="1910093" snippet_file_name="blog_20161001_25_3427720" class="other" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">注意加上--enable-fastcgi參數(shù)和--enable-force-cgi-redirect參數(shù)不加上的話配置完之后解析不了php
到這里整個LNMP已經(jīng)安裝完成.下面我們就配置php和nginx能運行php網(wǎng)站:
首先為php創(chuàng)建配置文件:
配置php-fpm,編輯php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf找到listen那一行,修改成如下內(nèi)容:
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的首頁文件}# 添加下面內(nèi)容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下創(chuàng)建index.php,
vi /usr/local/nginx/html/index.php添加下面內(nèi)容:
[php]?view plaincopy保存退出后訪問http://192.168.3.120/index.php,看到下面頁面表示已經(jīng)安裝配置成功:
總結(jié)
以上是生活随笔為你收集整理的lnmp环境搭建完全手册(四)——lnmp搭建(源码安装)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sudo apt-get update
- 下一篇: Leetcode-单调数列(896)