Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> 電腦軟件教程 >> 服務器技術 >> 關於服務器 >> LNMP服務器環境配置詳解(linux+nginx+mysql+php)

LNMP服務器環境配置詳解(linux+nginx+mysql+php)

日期:2017/2/8 10:11:46      編輯:關於服務器
對於初次買了一台vps的linux系統,現在邊學邊用吧,下面我整理了在linux系統中安裝nginx+php+mysql過程給各位同學參考,希望對大家會帶來幫助,具體如下。  

服務器配置

1.購買好vps後登錄服務器首先修改密碼passwd root
2.修改ssh的超時時間

 代碼如下   vi /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 30

3.安裝setup

 代碼如下  

#安裝setuptool
yum install setuptool
#可以發現執行setup後不全,再安裝一個用於系統服務管理
yum install ntsysv
#再安裝個防火牆,以及setup中配套的防火牆設置、網絡設置
yum install iptables
#安裝setup中配套的防火牆設置
yum install system-config-securitylevel-tui
#安裝setup中配套的網絡設置
yum install system-config-network-tui

4.格式化磁盤,掛載磁盤

客戶端環境:Mac OSX
遠程連接方式:運行 Terminal,輸入命令 ssh username@ip
硬盤分區及掛載操作步驟:
1. 查看未掛載的硬盤(名稱為/dev/xvdb)

 代碼如下   # fdisk -l
Disk /dev/xvdb doesn't contain a valid partition table
2. 創建分區
# fdisk /dev/xvdb
...
輸入n
Command (m for help):n
輸入p
Command action
e extended
p primary partition (1-4)
p
輸入1
Partition number (1-4): 1
回車
First cylinder (1-2610, default 1):
Using default value 1
回車
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Using default value 2610
輸入w
Command (m for help): w
The partition table has been altered!
3. 格式化分區
# mkfs.ext3 /dev/xvdb1
4. 建立掛載目錄
# mkdir /data
5. 掛載分區
# mount /dev/xvdb1 /data
6. 設置開機自動掛載
vi /etc/fstab
在vi中輸入i進入INERT模式,將光標移至文件結尾處並回車,將下面的內容復制/粘貼,然後按Esc鍵,輸入:x保存並退出 www.45it.com
/dev/xvdb1 /data ext3 defaults 0 0
7. 確認是否掛載成功
重啟服務器
# reboot
查看硬盤分區
# df
/dev/xvdb1 20635700 176196 19411268 1% /data

5.首先安裝gcc
6.安裝nginx

 代碼如下   wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
tar zxvf pcre-8.31.tar.gz
cd pcre-8.31/
./configure
make && make install
cd ../
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/web/soft/pcre-8.31':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
如果出現這樣的錯誤,則表示系統沒有編譯gcc
yum install gcc即可解決此問題
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] 錯誤 1
make[1]: Leaving directory `/web/soft/pcre-8.31'
make: *** [all] 錯誤 2
缺少gcc-c++
yum install gcc-c++
可編譯通過

二.安裝nginx

 代碼如下  

官方網址:http://www.nginx.org/
wget http://www.nginx.org/download/nginx-1.2.5.tar.gz
tar zxvf nginx-1.2.5.tar.gz
cd nginx-1.2.5/
./configure --user=www --group=www --prefix=/web/server/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
錯誤提示總結:
安裝提示錯誤libpcre.so.1,則需要執行命令ln -s /usr/local/lib/libpcre.so.1 /lib64
如果沒有安裝pcre庫,則在編譯安裝nginx的時候會提示./configure: error: the HTTP rewrite module requires the PCRE library.
如果提示錯誤nginx: [emerg] getpwnam("www") failed 則表示系統中沒有www用戶 用groupadd -f www useradd -g www www
如果提示錯誤./configure: error: SSL modules require the OpenSSL library. 則需要安裝openssl
ssl官網地址:http://www.openssl.org/source/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
編譯安裝ssl後重新安裝nginx
./configure --user=www --group=www --prefix=/web/server/nginx --with-http_stub_status_module --with-openssl=/usr/local/ssl
如果系統沒有安裝gcc,則需要先安裝gcc. 命令yum install gcc*
其他錯誤:
編譯Nginx提示gzip module requires the zlib library
新手在嘗試安裝nginx時,常常會因為缺少依賴組件,導致nginx相應模塊無法安裝,在執行“./configure”時常出現錯誤。
若在“./configure”後方加入了“--with-http_gzip_static_module”(添加gzip壓縮模塊)提示以下錯誤:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using –with-zlib= option.
則需要安裝“zlib-devel”即可。SSH執行以下命令:
yum install -y zlib-devel或者去官方網站下載zlib安裝wget http://zlib.net/zlib-1.2.7.tar.gz


 

7.安裝mysql

 

 代碼如下    rpm -ivh mysql.rpm

8.安裝php參見

 

 

安裝php5.4.0,編譯安裝php-5.4.0 ,php5.4.0 Zend Guard Loader暫時不支持,等待zend官方更新。

 代碼如下   1.下載
wget wget http://cn.php.net/distributions/php-5.4.0.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.7.5-9.tar.gz
wget http://pecl.php.net/get/imagick-3.0.1.tgz2.編譯安裝PHP 5.4.0所需的支持庫
wget http://pecl.php.net/get/memcache-3.0.7.tgz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make
make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
3.編譯mysql5.5.21
wget http://mysql.stu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.21.tar.gz
wget http://www.cmake.org/files/v2.8/cmake-2.8.7.tar.gz
tar xvzf cmake-2.8.7.tar.gz
./configure
make
make install
編譯安裝mysql
tar xvzf mysql-5.5.21.tar.gz
cd mysql5.5.21
rm CMakeCache.txt
cmake .
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.5.21/
-DMYSQL_DATADIR=/usr/local/mysql5.5.21/data
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/usr/local/mysql5.5.21/data/mysql.sock
-DMYSQL_USER=mysql
-DWITH_DEBUG=0
make && make install 4.編譯php
安裝依賴包
yum install ncurses-devel libxml2-devel bzip2-devel libcurl-devel curl-devel libjpeg-devel libpng-devel freetype-devel net-snmp-devel
tar xvzf php-5.4.0.tar.gz
cd php-5.4.0
./configure --prefix=/web/server/php --with-config-file-path=/web/server/php/etc --with-mysql --with-mysqli --with-pdo-mysql --with-iconv-dir=/usr/local --enable-fpm --disable-phar --with-fpm-user=www-data --with-fpm-group=www-data --with-pcre-regex --with-zlib --with-bz2 --enable-calendar --with-curl --enable-dba --with-libxml-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-mhash --enable-mbstring --with-mcrypt --enable-pcntl --enable-xml --disable-rpath --enable-shmop --enable-sockets --enable-zip --enable-bcmath --with-snmp --disable-ipv6
make ZEND_EXTRA_LIBS='-liconv'
make test
make install
cp php.ini-production /usr/local/php5.4.0/etc/php.ini5.編譯安裝PHP5擴展模塊
如果運行/web/server/php/bin/phpize,出現如下提示,則要安裝m4 autoconf
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
yum install autoconf
yum install m4
tar zxvf memcache-3.0.7.tgz
cd memcache-3.0.7/
/web/server/php/bin/phpize
./configure --with-php-config=/web/server/php/bin/php-config
make
make install
cd ../
/*暫時沒有編譯成功*/
tar xvzf memcached-1.0.2.tgz
cd memcached-1.0.2
/web/server/php/bin/phpize
./configure --with-php-config=/web/server/php/bin/php-config
make
make install
cd ../
/*沒有編譯*/
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../
/*沒有編譯*/
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php5.4.0/bin/phpize
./configure --with-php-config=/usr/local/php5.4.0/bin/php-config
make
make install
cd ../6.修改配置
vi /web/server/php/etc/php.ini
最後增加:
;change for hugwww
extension_dir = /usr/local/php5.4.0/lib/php/extensions/no-debug-non-zts-20090626/
extension = "memcache.so"
extension = "memcached.so"
extension = "imagick.so"
修改為:
cgi.fix_pathinfo=0
open_basedir = /tmp:/www/web
expose_php = Off
allow_url_fopen = Off
disable_functions =phpinfo,passthru,ini_restore,eval完成!

.開機啟動參見

編輯/etc/rc.d/rc.local文件,加入要啟動的文件即可

 代碼如下   /web/server/nginx/sbin/nginx
/web/server/php/sbin/php-fpm
/web/server/subversion/bin/svnserve -d -r /web/svn/repos
/web/server/memcached/bin/memcached -d -m 100 -u www -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
/web/server/apache/bin/apachectl

以上是啟動nginx服務
啟動php服務
啟動svn服務
啟動memcached服務

Copyright © Windows教程網 All Rights Reserved