LNMP環境的基礎搭建,即Linux、Nginx、MySQL、PHP環境

LNMP環境的基礎搭建,即Linux、Nginx、MySQL、PHP環境



注意:#用“//---//” 標記的部分不要。


參考內容:
lnmp環境搭建完全手冊(四)——lnmp搭建(源碼安裝)
http://blog.csdn.net/stuartjing/article/details/8124491




nginx的安裝
http://www.cnblogs.com/kingtigerhu/articles/2271270.html




nginx php-fpm安裝配置
http://linux.it.net.cn/e/server/nginx/2014/1208/9850.html





#查看linux版本
[root@localhost ~]# lsb_release -a
LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description:    CentOS release 5.5 (Final)
Release:        5.5
Codename:       Final


#嘗試用yum安裝nginx,結果沒有。
[root@localhost ~]# yum -y install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.skyshe.cn
 * base: mirrors.skyshe.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
addons                                                   | 1.9 kB     00:00     
base                                                     | 1.1 kB     00:00     
extras                                                   | 2.1 kB     00:00     
updates                                                  | 1.9 kB     00:00     
Setting up Install Process
No package nginx available.
Nothing to do


[root@localhost ~]# 




nginx: Linux packages
http://nginx.org/en/linux_packages.html#stable


選擇CentOS 5的stable version


[root@localhost ~]# cd /mnt/hgfs/sharefiles/nginx
[root@localhost nginx]# ls
nginx-1.9.4.tar.gz  nginx-release-centos-5-0.el5.ngx.noarch.rpm
[root@localhost nginx]# rpm -ivh nginx-release-centos-5-0.el5.ngx.noarch.rpm 
warning: nginx-release-centos-5-0.el5.ngx.noarch.rpm: Header V3 RSA/SHA1 signature: NOKEY, key ID 7bd9bf62
Preparing...                ########################################### [100%]
   1:nginx-release-centos   ########################################### [100%]
[root@localhost nginx]# which nginx
/usr/bin/which: no nginx in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost nginx]# 


[root@localhost nginx-1.9.4]# ./configure


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.




[root@localhost nginx-1.9.4]# yum -y install pcre
Updated:
  pcre.i386 0:6.6-9.el5                                                         


Complete!


[root@localhost nginx-1.9.4]# ./configure


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.




nginx的安裝
http://www.cnblogs.com/kingtigerhu/articles/2271270.html


[root@localhost nginx-1.9.4]# yum -y install pcre-devel
Installed:
  pcre-devel.i386 0:6.6-9.el5                                                   


Complete!




[root@localhost nginx-1.9.4]# yum -y install openssl-devel


....


Dependency Updated:
  e2fsprogs.i386 0:1.39-37.el5                                                  
  e2fsprogs-libs.i386 0:1.39-37.el5                                             
  krb5-libs.i386 0:1.6.1-80.el5_11                                              
  krb5-workstation.i386 0:1.6.1-80.el5_11                                       
  libselinux.i386 0:1.33.4-5.7.el5                                              
  libselinux-python.i386 0:1.33.4-5.7.el5                                       
  libselinux-utils.i386 0:1.33.4-5.7.el5                                        
  openssl.i686 0:0.9.8e-36.0.1.el5_11                                           
  zlib.i386 0:1.2.3-7.el5                                                       


Complete!




[root@localhost nginx-1.9.4]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README




[root@localhost nginx-1.9.4]# ./configure --prefix=/usr/local/nginx




[root@localhost nginx-1.9.4]# make




[root@localhost nginx-1.9.4]# make install


[root@localhost nginx-1.9.4]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@localhost nginx-1.9.4]# /usr/local/nginx/sbin/nginx


[root@localhost nginx-1.9.4]# netstat -antlp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      12310/nginx         
tcp        0      0 :::22                       :::*                        LISTEN      3804/sshd   
        
[root@localhost nginx-1.9.4]# 




#瀏覽器
http://127.0.0.1




網頁顯示:
Welcome to nginx!


If you see this page, the nginx web server is successfully installed and working. Further configuration is required.


For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.


Thank you for using nginx.




[root@localhost /]# yum install mysql-server php php-devel php-mysql


Installed:
  mysql-server.i386 0:5.0.95-5.el5_9      php.i386 0:5.1.6-45.el5_11           
  php-devel.i386 0:5.1.6-45.el5_11        php-mysql.i386 0:5.1.6-45.el5_11     


Dependency Installed:
  mysql.i386 0:5.0.95-5.el5_9            perl-DBD-MySQL.i386 0:3.0007-2.el5     
  perl-DBI.i386 0:1.52-2.el5             php-cli.i386 0:5.1.6-45.el5_11         
  php-common.i386 0:5.1.6-45.el5_11      php-pdo.i386 0:5.1.6-45.el5_11         


Complete!


#首次啓動mysql
[root@localhost /]# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK


To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'


Alternatively you can run:
/usr/bin/mysql_secure_installation


which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl


Please report any problems with the /usr/bin/mysqlbug script!


The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
You have mail in /var/spool/mail/root




#mysql數據庫端口
[root@localhost /]# netstat -antlp ¦ grep 3306
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17863/mysqld        
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      3565/portmap        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      12310/nginx         
tcp        0      0 0.0.0.0:821                 0.0.0.0:*                   LISTEN      3607/rpc.statd      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3813/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3914/sendmail: acce 
tcp        0      1 192.168.3.151:38521         74.125.203.190:443          SYN_SENT    12368/firefox       
tcp        0      0 :::22                       :::*                        LISTEN      3804/sshd           
tcp        0      0 ::ffff:192.168.3.151:22     ::ffff:192.168.3.10:55532   ESTABLISHED 4672/1              


#查看nginx配置
[root@localhost /]# cat /usr/local/nginx/conf/nginx.conf


#配置主機名
[root@localhost /]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
192.168.3.151   localhost.localdomain localhost


~
~
"/etc/hosts" 8L, 238C written


[root@localhost /]# 


#重啓nginx
[root@localhost sbin]#  /usr/local/nginx/sbin/nginx -s reload


#重啓nginx失敗了,只好重啓linux
[root@localhost /]# reboot




[root@localhost /]# 




#先檢查一下是否有問題
[root@localhost ~]# /usr/local/nginx/sbin/nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful








//-----------------------------------------------------------------//
#編寫nginx的啓動腳本,並加入系統服務
#這個文件後來刪掉了,因爲發現隨系統啓動後,stop失敗,也無法重啓。只能reboot系統纔行。




[root@localhost ~]# vi /etc/init.d/nginx


編寫nginx的啓動腳本,並加入系統服務
vi /etc/init.d/nginx寫入以下內容:
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/var/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}
stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}
reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
restart(){
        stop
        start
}
configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac


exit $RETVAL








保存後,更改/etc/init.d/nginx的權限
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on




//-----------------------------------------------------------------//


#啓動nginx
[root@localhost ~]# service nginx start
Starting Nginx:                                            [  OK  ]


[root@localhost ~]# netstat -antlp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4724/nginx.conf 




#如果啓動不了,請到/usr/local/nginx/logs/目錄下查看nginx_error.log這個日誌文件。若是沒有這個日誌文件,很有可能是那個目錄沒有寫權限,請執行


chmod +w /usr/local/nginx/logs/
service  nginx  restart


[root@localhost ~]# chmod +w /usr/local/nginx/logs/






//-----------------------------------------------------------------//
#發現問題,nginx啓動後網頁無法訪問
#關鍵文件要備份啊,幸好我有個備份nginx (copy).conf,後來是採用還原nginx.conf這個文件,nginx就恢復了。


[root@localhost local]# cd /usr/local/
[root@localhost local]# cat error.log 


#解決 connect() to unix:/var/run/php-fpm/php-fpm.sock failed (2: No such file or directory) 問題


[root@localhost local]# yum -y install gcc automake autoconf libtool make


[root@localhost local]# yum -y install gcc gcc-c++ glibc




[root@localhost local]# yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-deve


#php版本
[root@localhost sharefiles]# php -v
PHP 5.1.6 (cli) (built: Nov  6 2014 12:25:33) 
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies


#想打個補丁,不過失敗了
[root@localhost sharefiles]# gzip -cd php-5.2.17-fpm-0.5.14.diff.gz 
//-----------------------------------------------------------------//








php-fpm介紹及配置
http://blog.csdn.net/tonyxf121/article/details/7869187




#php-fpm配置


從php5.3.3版本開始就已經集成了fpm,不再是第三方的包了,如果使用的是php5.3.3以前的版本就得自己去安裝fpm包了,對於php5.2.x的版本,fpm的配置文件還是xml格式,php5.3.x開始fpm配置文件


就已經支持ini格式了。




tar用法:
.tar.gz     格式解壓爲          tar   -zxvf   xx.tar.gz


.tar.bz2   格式解壓爲          tar   -jxvf    xx.tar.bz2


[root@localhost local]# 


#不打升級包了,直接更新php




[root@localhost sharefiles]# tar -jxvf php-5.6.13.tar.bz2 


[root@localhost sharefiles]#ls


[root@localhost sharefiles]#cd php-5.6.13


#一開始我是用./configure 和 make && make install安裝,後來發現不行。
#./configure 後面一定要帶參數


[root@localhost php-5.6.13]# mkdir /etc/php /etc/php5.d
[root@localhost php-5.6.13]# cp php.ini-production /etc/php
[root@localhost php-5.6.13]#  chmod a+x /etc/init.d/php-fpm


[root@localhost php-5.6.13]# ./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir


[root@localhost php-5.6.13]# make all install




nginx 服務器重啓命令,關閉 
http://blog.chinaunix.net/uid-24960107-id-341855.html






[root@localhost ~]# cd /usr/local/nginx/


[root@localhost nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp


[root@localhost nginx]# cd sbin


[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx 


[root@localhost sbin]# netstat -antlp ¦ grep 80
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      3564/portmap        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4339/nginx          
tcp        0      0 0.0.0.0:820                 0.0.0.0:*                   LISTEN      3606/rpc.statd      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3812/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3912/sendmail: acce 
tcp        0      0 :::22                       :::*                        LISTEN      3803/sshd           
tcp        0    596 ::ffff:192.168.3.151:22     ::ffff:192.168.3.10:52560   ESTABLISHED 4293/1              




#停止niginx
[root@localhost sbin]# ./nginx -s stop


#
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 










下面是對php-fpm運行用戶進行設置


cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf  
修改
user = oracle
group = oracle


如果oracle用戶不存在,那麼先添加oracle用戶
groupadd oracle
useradd -g oracle oracle




修改nginx配置文件以支持php-fpm


nginx安裝完成後,修改nginx配置文件爲:nginx.conf 


其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}






#以防以後忘記了,這裏把server這段內容貼出來
##開始
   server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;   (這裏漏了加index.php,不過php頁面也正確顯示出來了。)




# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}




        }


##結束








啓動php-fpm和nginx


#重啓nginx
[root@localhost nginx]# ./sbin/nginx  -s stop
[root@localhost nginx]# ./sbin/nginx




#找php-fpm目錄
[root@localhost php]# ls
bin  etc  include  lib  man  php  php.ini  sbin  var
[root@localhost php]# cd /usr/local/php/
[root@localhost php]# ls
bin  etc  include  lib  man  php  php.ini  sbin  var
[root@localhost php]# cd sbin/
[root@localhost sbin]# ls
php-fpm


#啓動php-fpm
[root@localhost sbin]# ./php-fpm 
[root@localhost sbin]# 




瀏覽器:http://127.0.0.1/index.php
頁面顯示:PHP Version 5.6.13






mysql 的設置和LAMP中的設置一樣,設置用戶密碼,刪除匿名用戶。
測試PHP與mysql連接。
這裏就不再重複了。








不過以後要記得LNMP需要啓動的服務:


./nginx
./php-fpm
/etc/init.d/mysqld start (mysql登錄: mysql -u root)




結束測試:

[root@localhost ~]# halt


Broadcast message from root (pts/4) (Fri Sep 11 06:48:59 2015):


The system is going down for system halt NOW!




總結:

1. 手動啓服務

#啓動mysql
[root@localhost /]# service mysqld start

#nginx啓動
[root@localhost sbin]# ./nginx 


#停止niginx

[root@localhost sbin]# ./nginx -s stop


#啓動php-fpm
[root@localhost sbin]# ./php-fpm 




2.檢查nginx

#檢查nginx是否有問題
[root@localhost ~]# /usr/local/nginx/sbin/nginx  -t


3.配置文件:

nginx配置文件:/etc/init.d/nginx



//-----------------------------------------------------------------------------------------------------//

本文只是簡單搭建了一個測試環境。如果需要用好nginx還需要不斷學習和研究。


詳解Nginx配置文件結構

http://www.linuxidc.com/Linux/2013-11/92594.htm



























#nginx啓動
[root@localhost sbin]# ./nginx 


#停止niginx

[root@localhost sbin]# ./nginx -s stop


#啓動php-fpm
[root@localhost sbin]# ./php-fpm 


#啓動mysql

/etc/init.d/mysqld start


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章