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相关路径应根据实际环境改变


你可能感兴趣的:(Linux,PHP,数据库,Lnmp,Linux)