Linux(centos)下LNMP環境安裝

Lnmp環境安裝
1.安裝相關依賴
使用 yum 程序安裝所需開發包(以下爲標準的rpm包名稱)
yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel mysql-devel net-snmp-devel curl-devel perl-DBI

一、編譯PHP
(1)指定PHP相關配置目錄和拓展,進入已經解壓好的源碼包裏,檢查安裝環境
	./configure --prefix=/usr/local/php \
	--exec-prefix=/usr/local/php \
	--bindir=/usr/local/php/bin \
	--sbindir=/usr/local/php/sbin \
	--includedir=/usr/local/php/include \
	--libdir=/usr/local/php/lib/php \
	--mandir=/usr/local/php/php/man \
	--with-config-file-path=/usr/local/php/etc \
	--with-mysql-sock=/tmp/mysql.sock \
	--with-mysqli \
	--with-pdo-mysql \
	--with-mcrypt=/usr/include \
	--with-mhash \
	--with-openssl \
	--with-gd \
	--with-iconv \
	--with-zlib \
	--enable-zip \
	--enable-inline-optimization \
	--disable-debug \
	--disable-rpath \
	--enable-shared \
	--enable-xml \
	--enable-bcmath \
	--enable-shmop \
	--enable-sysvsem \
	--enable-mbregex \
	--enable-mbstring \
	--enable-ftp \
	--enable-gd-native-ttf \
	--enable-pcntl \
	--enable-sockets \
	--with-xmlrpc \
	--enable-soap \
	--without-pear \
	--with-gettext \
	--enable-session \
	--with-curl \
	--with-jpeg-dir \
	--with-freetype-dir \
	--enable-opcache \
	--enable-fpm \
	--without-gdbm \
	--disable-fileinfo

(2)make & make install

3. 複製安裝包內的配置文件(php.ini-develoment/php.ini-production)到安裝目錄/usr/local/php/lib目錄下
4. 修改PHP進程文件:/etc/php-fpm.conf.default => etc/php-fpm.conf
5. 啓動php的管理進程/usr/local/php/sbin/php-fpm
6. 查看php進程
	[root@localhost php]# ps aux | grep php
	root     14760  0.0  0.6 199736  6292 ?        Ss   15:36   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
	www      14761  0.0  0.5 199736  5744 ?        S    15:36   0:00 php-fpm: pool www
	www      14762  0.0  0.5 199736  5748 ?        S    15:36   0:00 php-fpm: pool www
	root     14764  0.0  0.0 103256   856 pts/1    S+   15:36   0:00 grep php

7.設置開機啓動
(1) vi /etc/init.d/php-fpm
#!/bin/sh  
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  

stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  

restart() {  
    configtest || return $?  
    stop  
    start  
}  

reload() {  
    configtest || return $?  
    echo -n $"Reloading $prog: "  
    killproc ${phpfpm} -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

rh_status() {  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2  
esac

(2) 設置php-fpm文件可執行權限
	chmod a+x /etc/init.d/php-fpm

(3) 將php-fpm 加入到系統開機管理列表中
	chkconfig --add /etc/init.d/php-fpm

(4) 設置開機啓動
	chkconfig php-fpm on

(5) 查看當前php-fpm啓動狀態
	chkconfig php-fpm status	

相關錯誤解決
(1)啓動PHP進程時,報錯
[root@localhost php]# ./sbin/php-fpm 
[06-Jan-2018 15:14:41] WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/*.conf' from /usr/local/php/etc/php-fpm.conf at line 125.
[06-Jan-2018 15:14:41] ERROR: No pool defined. at least one pool section must be specified in config file
[06-Jan-2018 15:14:41] ERROR: failed to post process the configuration
[06-Jan-2018 15:14:41] ERROR: FPM initialization failed

解決辦法:
	[root@localhost php]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
(2)啓動PHP進程時,報錯,是因爲有了
--with-fpm-user=www \
--with-fpm-group=www \ 這兩個配置項的存在,這兩個配置也可以不加入,在安裝的時候
[06-Jan-2018 15:22:31] ERROR: [pool www] cannot get uid for user 'www'
[06-Jan-2018 15:22:31] ERROR: FPM initialization failed
	報錯原因: 系統內沒有www用戶和www組
解決辦法:
	[root@localhost php]# useradd www
	[root@localhost php]# groupadd www

二、編譯Nginx
(1)進入nginx安裝包目錄,安裝之前記得先安裝pcre(正大表達式庫)
	./configure --prefix=/usr/local/nginx,安裝完目錄後如下
	[root@localhost nginx-1.13.8]# cd /usr/local/nginx/
	[root@localhost nginx]# ls
	conf(配置文件)  html(網頁文件)  logs(日誌文件)  sbin(主要二進制文件)

Nginx相關使用命令:

1.查看Nginx進程信息:ps aux | grep nginx
	[root@localhost nginx]# ps aux | grep nginx
	root     17290  0.0  0.0  20476   656 ?        Ss   16:04   0:00 nginx: master process ./sbin/nginx
	nobody   17291  0.0  0.1  20920  1256 ?        S    16:04   0:00 nginx: worker process
	root     17293  0.0  0.0 103256   856 pts/1    S+   16:04   0:00 grep nginx

2.結束進程
	kill -9 [pid號]: kill -9 2789
	pkill -9 [進程名稱]: pkill -9 http
	kill -INT 進程號:kill -INT 17290 => 殺掉Nginx的主進程
	/sbin/nginx -s stop 

3.重啓
	./sbin/Nginx -s reload
	kill -HUP `cat logs/nginx.pid`

4.檢測Nginx配置文件是否有語法錯誤
	./sbin/nginx -t

5.nginx相關知識網站:https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/
6.設置nginx開機自啓動
(1)vi /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

(2) 設置/etc/init.d/nginx可執行權限
	chmod a+x /etc/init.d/nginx

(3) 將nginx服務加入到Linux系統開機服務管理列表
	chkconfig --add /etc/init.d/nginx

(4) 設置nginx開機啓動
	chkconfig nginx on	

(5) 查看系統開機服務nginx服務是否在其中
	chkconfig

(6) 查看nginx服務的狀態
	service nginx status

(7) 轉換nginx各種狀態
	service nginx stop/start/restart/reload/status

三、PHP與nginx進行整合,使其兩者能夠進行相互通信,修改nginx的配置文件
ps: php-fpm 進程的端口號是9000,所以是127.0.0.1:9000
 location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
    include        fastcgi_params;
}

四、編譯MySQL(MariaDB)
1.解壓tar安裝包
2.輸入編譯參數
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/data/mysql \
 -DSYSCONFDIR=/etc \
 -DWITHOUT_TOKUDB=1 \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 \
 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
 -DWIYH_READLINE=1 \
 -DWIYH_SSL=system \
 -DVITH_ZLIB=system \
 -DWITH_LOBWRAP=0 \
 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci

3.編譯錯誤 
	cc: Internal error: Killed (program cc1
	原因:系統內存不足
解決:
	cc: internal compiler error: Killed (program cc1)
	或者
	g++: internal compiler error: Killed (program cc1plus)
	Please submit a full bug report,
 
	主要原因大體上是因爲內存不足,有點坑 臨時使用交換分區來解決吧,交換分區即將磁盤分區當做虛擬內存來使用,使用完之後,再把虛擬分區刪除掉就好了
	創建 swap 交換分區
	dd if=/dev/zero of=/swapfile bs=64M count=16
	mkswap /swapfile
	swapon /swapfile
	下面是執行 make 命令過程中內存使用情況

	編譯過程。。。。。。。。。。。。。。。。。。。。。。。

	結束後關閉交換分區
	swapoff /swapfile
	rm /swapfile

4. 使用 `mysql` 用戶執行腳本, 安裝數據庫到數據庫存放目錄
	[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
	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 MariaDB root USER !
	To do so, start the server, then issue the following commands:

	'./bin/mysqladmin' -u root password 'new-password'
	'./bin/mysqladmin' -u root -h localhost password 'new-password'

	Alternatively you can run:
	'./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 MariaDB Knowledgebase at http://mariadb.com/kb or the
	MySQL manual for more instructions.

	You can start the MariaDB daemon with:
	cd '.' ; ./bin/mysqld_safe --datadir='/data/mysql'

	You can test the MariaDB daemon with mysql-test-run.pl
	cd './mysql-test' ; perl mysql-test-run.pl

5.啓動MySQL
	/etc/rc.d/init.d/mysqld start

6. 在開發測試階段需要進行遠程登錄連接數據庫,此時可以授權一個用戶
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
username: linux系統中的一個用戶,或者插入到mysql數據庫中user表裏已經存在的用戶
password:數據庫客戶端鏈接數據庫的設置的 

7. 設置MySQL開機啓動
(1)	複製文件並重命名
	cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 

(2) 將MySQL加入到系統自啓動管理列表中
	chkconfig --add mysql

(3) 設置開啓啓動
	chkconfig mysql on

(4) 顯示服務列表
	chkconfig

(5) 如果看到mysql的服務,並且3,4,5都是on的話則成功,如果是off,則鍵入
	chkconfig --level 345 mysql on

(6) 查看3306端口
	netstat -an | grep mysql

五、編譯Redis
1. 下載源碼包:wget http://download.redis.io/releases/redis-4.0.6.tar.gz
2. 解壓源碼包: tar zxvf redis-4.0.6.tar.gz,進入解壓目錄 
3. Redis的源碼包是已經編譯配置過的,所以直接make就可以:make
4. 檢查Redis編譯安裝環境(可選): make test 
5. 安裝:make install PREFIX=/usr/local/redis Redis安裝目錄可選
6. 複製配置文件到安裝目錄: cp /usr/local/src/redis-4.0.6/redis.conf ./
7. 啓動Redis: ./bin/redis-server ./redis.conf 
8. 連接Redis客戶端:./bin/redis-cli
9. 設置Redis開機啓動
(1)編輯文件: vi /etc/init.d/redis
#!/bin/sh
#
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac


(2) 設置redis文件的權限
	chmod 755 /etc/init.d/redis

(3) 將redis服務加入到系統開機啓動列表中
	chkconfig --add redis

(4) 設置開機啓動
	chkconfig redis on

(4) /etc/init.d/redis中的redis相關路徑應根據實際環境改變


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