LAMP介绍,MySQL、MariaDB介绍 MySQL安装,安装mariadb,安装Apache

LAMP介绍

LAMP 是 linux + Apache(httpd) + MySQL + PHP

pache(httpd) + MySQL + PHP三个能够在一台机器上,也能够分开,。可是Apache(httpd)和PHP是一只都在一块儿的。php

httpd 、PHP、MySQL三者如何工做。

用户浏览器————>Apache————>PHP ————>MySQLhtml

Apache读取分为动态文件和静态文件。mysql

动态文件,存在MySQL。如密码,帖子,linux

静态文件,图片等,存在linux服务器里。web

读取动态文件,须要经过PHP再到MySQL。sql


MySQL/Mariadb介绍

MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司收购(10亿刀),2009年sun公司被oracle 公司收购(74亿刀)数据库

MySQL官网https://www..com 最新版本5.7GA/8.0DMRapache

MySQL5.6变化较大,5.7性能上有很大提高windows

Mariadb 为MySQL的一个分支,官网https://mariadb.com/最新版本10.3api

MariaDB主要由SkySQL公司(现改名为MariaDB公司)维护,SkySQL公司由MySQL原做者带领大部分原班人马创立。

Mariadb5.5版本对应MySQL的5.5,10.0对应MySQL5.6

Community 社区版本,Enterprise 企业版,GA 指通用版本,在生产环境中用的,DMR开发里程碑发布版,RC发行候选版本,Beta开放测试版本,Alpha内部测试版本


安装MySQL

  • MySQL的几个经常使用安装包:rpm、源码、二进制免编译

  • 下载二进制免编译包(二进制包要下载到/usr/local/src/下)

[root@aminglinux-01 ~]# cd /usr/local/src/
[root@aminglinux-01 src]# ls
httpd-2.2.32  httpd-2.2.32.tar.gz

[root@aminglinux-01 src]# 
[root@aminglinux-01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

地址在r.aminglinux.com找

  • 解压

tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  • 改一下文件目录的名字

[root@aminglinux-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
  • 到这个目录下 建立一个mysql的用户 ,在建立一个数据目录

[root@aminglinux-01 src]# cd /usr/local/mysql/
[root@aminglinux-01 mysql]# useradd mysql
[root@aminglinux-01 mysql]# mkdir /data/   (若是目录已经存在无需建立)
  • 而后初始化一下,指定用户为mysql,指定数据目录

[root@aminglinux-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
[root@aminglinux-01 mysql]#

提示缺乏Perl模块

能够百度或者google搜索问题。

或者搜索一下yum包:yum list |grep perl |grep -i dumper

[root@aminglinux-01 mysql]# yum list |grep perl |grep -i dumper
perl-Data-Dumper.x86_64                 2.145-3.el7                    base     
perl-Data-Dumper-Concise.noarch         2.020-6.el7                    epel     
perl-Data-Dumper-Names.noarch           0.03-17.el7                    epel     
perl-XML-Dumper.noarch                  0.81-17.el7                    base     
[root@aminglinux-01 mysql]#

若是不肯定是哪一个包就都安装上

其实是依赖这个包: perl-Data-Dumper.x86_64

而后再进行初始化,进行指定

  • 报错
root@bigdata-159:/usr/local/mysql# ./bin/mysqld -- defaults-file=/etc/my.cnf --initialize --user=mysql
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
  • 解决方法:
[root@example.com data]# yum install -y libaio  //安装后在初始化就OK了

继续初始化

Please report any problems at http://bugs.mysql.com/

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

New default config file was created as ./my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[root@aminglinux-01 mysql]#

能够echo $?一下,若是是0 ,证实没有问题。初始化成功

[root@aminglinux-01 mysql]# echo $?
0
  • 拷贝配置文件和启动脚本

配置文件在 support-files/

[root@aminglinux-01 mysql]# ls support-files/
binary-configure  magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@aminglinux-01 mysql]#

模板配置文件: my-default.cnf

拷贝 默认配置文件就放到/etc/my.cnf

[root@aminglinux-01 mysql]# ls support-files/
binary-configure  magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@aminglinux-01 mysql]# cp support-files/my-default.cnf  /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"?

若是以前就有了,多是哪个rpm包在安装时候生成的,能够rpm -qf /etc/my.cnf查看一下是那个rpm包生成的。也可使用。可是须要改一下配置文件。

如下是自带my.cnf文件的修改。

[root@aminglinux-01 mysql]# vi /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

~                                                                                                                                          
~                                                                                                                                          
~                                                                                                                                          


"/etc/my.cnf" 19L, 570C

改过以后

[mysqld]
datadir=/data/mysql      改
socket=/tmp/mysql.sock    改
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log     改
#pid-file=/var/run/mariadb/mariadb.pid      改

#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d                 改

~
  • 启动脚本也在support-files/下面

须要copy [root@aminglinux-01 mysql]# cp support-files/mysql.server

[root@aminglinux-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@aminglinux-01 mysql]#

而后进行编辑

[root@aminglinux-01 mysql]# vi /etc/init.d/mysqld

须要修改几个地方。

# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql       须要修改
datadir=/data/mysql           须要修改

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

权限变动,755。默认也是755

[root@aminglinux-01 ~]# ls -l /etc/init.d/mysqld 
-rwxr-xr-x. 1 root root 10907 9月  27 09:42 /etc/init.d/mysqld
[root@aminglinux-01 ~]#

加入系统服务里

[root@aminglinux-01 ~]# chkconfig --add mysqld
[root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@aminglinux-01 ~]#

能够手动起这个服务

[root@aminglinux-01 ~]# service mysqld start
Starting MySQL.Logging to '/data/mysql/aminglinux-01.err'.
.. SUCCESS! 
[root@aminglinux-01 ~]#

查看进程和端口

[root@aminglinux-01 ~]# ps aux |grep mysql 
root       2372  0.0  0.1  11760  1600 pts/0    S    09:52   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-01.pid
mysql      2507  2.5 45.5 973048 455352 pts/0   Sl   09:52   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-01.err --pid-file=/data/mysql/aminglinux-01.pid --socket=/tmp/mysql.sock
root       2536  0.0  0.0 112664   976 pts/0    R+   09:54   0:00 grep --color=auto mysql
[root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      910/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1212/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2507/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      910/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1212/master         
[root@aminglinux-01 ~]#

若是启动脚本有问题或者没有启动模板等还有另外一种启动方法。/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &

[root@aminglinux-01 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@aminglinux-01 ~]# !ps
ps aux |grep mysql 
root       2619  0.0  0.0 112664   976 pts/0    S+   10:00   0:00 grep --color=auto mysql
[root@aminglinux-01 ~]# /usr/local/mysql/bin/mysqld_safe  --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &(&符号是把命令丢到后台去)
[1] 2648


[root@aminglinux-01 ~]# 170927 10:03:24 mysqld_safe Logging to '/data/mysql/aminglinux-01.err'.
170927 10:03:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql

[root@aminglinux-01 ~]# !ps
ps aux |grep mysql 
root       2648  0.0  0.1 113256  1588 pts/0    S    10:03   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
mysql      2771  0.7 45.0 973048 450268 pts/0   Sl   10:03   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-01.err --pid-file=/data/mysql/aminglinux-01.pid --socket=/tmp/mysql.sock
root       2804  0.0  0.0 112664   976 pts/0    R+   10:04   0:00 grep --color=auto mysql
[root@aminglinux-01 ~]#
--defaults-file=/etc/my.cnf 指定配置文件所在目录

命令行的开启只能用kill来进行关闭服务,用killall更安全,否则会致使数据在内存中还没写到磁盘里就杀掉了。killall 会先停掉,保存,而后杀掉。

[root@aminglinux-01 ~]# killall mysqld
[root@aminglinux-01 ~]# 170927 10:36:39 mysqld_safe mysqld from pid file /data/mysql/aminglinux-01.pid ended

[1]+  完成                  /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
[root@aminglinux-01 ~]# !ps
ps aux |grep mysql 
root       3013  0.0  0.0 112664   972 pts/0    R+   10:36   0:00 grep --color=auto mysql
[root@aminglinux-01 ~]#

若是进程杀了一分钟尚未杀掉,可能还在写数据,要在等一下子。


安装mariadb

[root@aminglinux-01 ~]# cd /usr/local/src 
[root@aminglinux-01 src]# wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bintar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
  • 下载好后,解压,移动位置

httpd-2.2.32         mariadb-10.2.6-linux-glibc_214-x86_64         mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
httpd-2.2.32.tar.gz  mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
[root@aminglinux-01 src]# mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb
[root@aminglinux-01 src]#
  • 到这个文件夹下初始化,建立用户,建立data

[root@aminglinux-01 mariadb]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mariadb
Installing MariaDB/MySQL system tables in '/data/mariadb' ...
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 aminglinux-01 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/mariadb'

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

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

[root@aminglinux-01 mariadb]# echo $?
0
[root@aminglinux-01 mariadb]#
  • 而后该copy配置文件了。

[root@aminglinux-01 mariadb]# cd /usr/local/mariadb/
[root@aminglinux-01 mariadb]# ls support-files/
binary-configure  my-huge.cnf             my-large.cnf   my-small.cnf         mysql-log-rotate  policy     wsrep_notify
magic             my-innodb-heavy-4G.cnf  my-medium.cnf  mysqld_multi.server  mysql.server      wsrep.cnf
[root@aminglinux-01 mariadb]#

这里面有好几个配置模板,怕跟以前装的mysql冲突,因此此次换个文件夹copy

[root@aminglinux-01 mariadb]# cp support-files/my-small.cnf  /usr/local/mariadb/my.cnf
[root@aminglinux-01 mariadb]#
  • 再copy一个启动脚本

[root@aminglinux-01 mariadb]# cp support-files/mysql.server /etc/init.d/mariadb
[root@aminglinux-01 mariadb]#

修改配置文件和启动脚本,配置文件修改的话,须要改mysqld, 基本上不用改。

启动脚本,须要修改

#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mariadb    修改
datadir=/data/mariadb         修改
conf=$basedir/my.cnf          新加,新加后须要在下面启动命令定义一下

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overridden by value in my.cnf.
# 0 means don't wait at all
case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe (须要加的)--defaults-file=$conf  --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
      wait_for_ready; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"

定义conf是为了避免跟以前安装的mysql出现冲突,通常不会有mysql和mariadb同时安装的状况,若是只有一个,能够不用定义。直接把my.cnf 放到etc下

  • 启动一下

[root@aminglinux-01 ~]# /etc/init.d/mariadb start
Starting mariadb (via systemctl):                          [  肯定  ]
[root@aminglinux-01 ~]# ps aux |grep mariadb
root       2542  0.0  0.1 115380  1740 ?        S    23:01   0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-01.pid
mysql      2658  5.9  5.8 1125120 58608 ?       Sl   23:01   0:01 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mysql --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-01.err --pid-file=/data/mysql/aminglinux-01.pid --socket=/tmp/mysql.sock --port=3306
root       2704  0.0  0.0 112664   972 pts/0    R+   23:01   0:00 grep --color=auto mariadb
[root@aminglinux-01 ~]#

安装Apache

  • #### Apache是一个基金会的名字,httpd才是咱们要安装的软件包,早期它的名字就叫apache

  • Apache官网www.apache.org

  • 安装Apache 2.4 源码包,安装须要手动编译apr,因此先下载下来

2.4源码包: http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.27.tar.gz

apr: http://mirrors.hust.edu.cn/apache/apr/apr-1.6.2.tar.gz

apr-util: http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.0.tar.bz2

  • 先解压

  • 而后安装 apr

apr和apr-util是一个通用的函数库,它让httpd 能够不关心底层的操做系统平台,能够很方便的移植(从linux移植到windows)

[root@aminglinux-01 apr-1.6.2]# ./configure --prefix=/usr/local/apr
[root@aminglinux-01 apr-1.6.2]# make && make install

安装下一个

[root@aminglinux-01 apr-1.6.2]# cd ../apr-util-1.6.0/
[root@aminglinux-01 apr-util-1.6.0]#  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@aminglinux-01 apr-util-1.6.0]# make && make install

出现报错

xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录

可能缺expat的开发库,yum install expat-devel解决

安装httpd2.4

[root@aminglinux-01 httpd-2.4.27]#  ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most

出现报错

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
[root@aminglinux-01 httpd-2.4.27]# yum list |grep pcre 
pcre.x86_64                             8.32-15.el7_2.1                @anaconda
ghc-pcre-light.x86_64                   0.4-13.el7                     epel     
ghc-pcre-light-devel.x86_64             0.4-13.el7                     epel     
mingw32-pcre.noarch                     8.38-1.el7                     epel     
mingw32-pcre-static.noarch              8.38-1.el7                     epel     
mingw64-pcre.noarch                     8.38-1.el7                     epel     
mingw64-pcre-static.noarch              8.38-1.el7                     epel     
pcre.i686                               8.32-17.el7                    base     
pcre.x86_64                             8.32-17.el7                    base     
pcre-devel.i686                         8.32-17.el7                    base     
pcre-devel.x86_64                       8.32-17.el7                    base     
pcre-static.i686                        8.32-17.el7                    base     
pcre-static.x86_64                      8.32-17.el7                    base     
pcre-tools.x86_64                       8.32-17.el7                    base     
pcre2.i686                              10.23-2.el7                    base     
pcre2.x86_64                            10.23-2.el7                    base     
pcre2-devel.i686                        10.23-2.el7                    base     
pcre2-devel.x86_64                      10.23-2.el7                    base     
pcre2-static.i686                       10.23-2.el7                    base     
pcre2-static.x86_64                     10.23-2.el7                    base     
pcre2-tools.x86_64                      10.23-2.el7                    base     
pcre2-utf16.i686                        10.23-2.el7                    base     
pcre2-utf16.x86_64                      10.23-2.el7                    base     
pcre2-utf32.i686                        10.23-2.el7                    base     
pcre2-utf32.x86_64                      10.23-2.el7                    base     
[root@aminglinux-01 httpd-2.4.27]#

一般运行库都是-devel或-lib,因此安装下pcre-devel.x86_64

[root@aminglinux-01 httpd-2.4.27]# yum -y install pcre-devel.x86_64
[root@aminglinux-01 httpd-2.4.27]#  ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
[root@aminglinux-01 httpd-2.4.27]# echo $?
0

make 安装

make

而后

make install

collect2: error: ld returned 1 exit status 错误提示

####### collect2: error: ld returned 1 exit status 错误提示 #####################
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: 离开目录“/usr/local/src/httpd-2.4.27/support”
make[1]: *** [all-recursive] 错误 1
make[1]: 离开目录“/usr/local/src/httpd-2.4.27/support”
make: *** [all-recursive] 错误 1

#### 解决办法 1 ####(我是用这个解决的)
[root@LONG httpd-2.4.27]# make clean
[root@LONG httpd-2.4.27]# cd ..
[root@LONG src]# rm -rf apr-1.6.2
[root@LONG src]# rm -rf apr-util-1.6.0
[root@LONG src]# rm -rf httpd-2.4.27
# 从新安装apr,apr-util,httpd

/usr/local/apache2.4/bin/httpd -M //查看加载的模块

启动Apache

[root@aminglinux-01 apache2]# /usr/local/apache2.4/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::dbd:48aa:6994:bf39. Set the 'ServerName' directive globally to suppress this message
[root@aminglinux-01 apache2]# ps aux |grep httpd
root      55858  0.0  0.2  95520  2532 ?        Ss   00:41   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    55859  0.0  0.4 382348  4428 ?        Sl   00:41   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    55860  0.0  0.4 382348  4428 ?        Sl   00:41   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    55861  0.0  0.4 382348  4428 ?        Sl   00:41   0:00 /usr/local/apache2.4/bin/httpd -k start
root      55963  0.0  0.0 112668   972 pts/0    S+   00:42   0:00 grep --color=auto httpd
[root@aminglinux-01 apache2]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      915/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1414/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2658/mysqld         
tcp6       0      0 :::80                   :::*                    LISTEN      55858/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      915/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1414/master         
[root@aminglinux-01 apache2]#

成功安装


PHP安装

  • PHP官网: www.php.net

  • 当前主流版本5.6/7.1

  • 安装

先下载

[root@aminglinux-01 ~]# wget http://cn2.php.net/distributions/php-5.6.30.tar.bz2
--2017-09-28 13:58:22--  http://cn2.php.net/distributions/php-5.6.30.tar.bz2
正在解析主机 cn2.php.net (cn2.php.net)... 202.108.35.250, 202.108.35.235
正在链接 cn2.php.net (cn2.php.net)|202.108.35.250|:80... 已链接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:15011816 (14M) [application/octet-stream]
正在保存至: “php-5.6.30.tar.bz2”

100%[=================================================================================================>] 15,011,816  3.70MB/s 用时 4.2s   

2017-09-28 13:58:27 (3.44 MB/s) - 已保存 “php-5.6.30.tar.bz2” [15011816/15011816])

[root@aminglinux-01 ~]#

解压: tar jxvf php-5.6.30.tar.bz2

编译php

[root@aminglinux-01 src]# cd php-5.6.30/
[root@aminglinux-01 php-5.6.30]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif

--prefix=/usr/local/php 指定安装目录

--with-apxs2=/usr/local/apache2.4/bin/apxs 这个是apache的一个工具,这个工具可让咱们不用人工的干涉它,会自动的帮把扩展的模块放到apache的modules,而且在配置文件里加上一行load ,modules

--with-config-file-path=/usr/local/php/etc 指定配置文件所在路径

--with-mysql=/usr/local/mysql 指定mysql的路径

报错:configure: error: xml2-config not found. Please check your libxml2 installation.

缺乏库:yum install -y libxml2-devel

又报错:configure: error: Cannot find OpenSSL's <evp.h>

yum install -y openssl-devel

报错:configure: error: Please reinstall the BZip2 distribution

yum install -y bzip2-devel

报错:configure: error: jpeglib.h not found.

yum install -y libjpeg-devel

报错:configure: error: png.h not found.

yum install -y libpng-devel

报错: configure: error: freetype-config not found.

yum install -y freetype-devel

报错: configure: error: mcrypt.h not found. Please reinstall libmcrypt.

yum install -y epel-release  先安装扩展源
 yum install -y libmcrypt-devel

出现这个就表明安装好啦

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
[root@aminglinux-01 php-5.6.30]#

安装:make

make install

copy配置文件(由于以前编译参数有定义过,因此须要改一下)

[root@aminglinux-01 php-5.6.30]# cp php.ini-production /usr/local/php/etc/php.ini

查看是否有配置文件

[root@aminglinux-01 php-5.6.30]# /usr/local/php/bin/php -i |less






PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
phpinfo()
PHP Version => 5.6.30

System => Linux aminglinux-01 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64
Build Date => Sep 28 2017 15:40:08
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-apxs2=/usr/local/apache2.4/bin/apxs' '--with-config-file-path=/usr/local/php/etc' '--with-mysql=/usr/local/mysql' '--with-pdo-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets' '--enable-exif'
Server API => Command Line Interface
Virtual Directory Support => enabled
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini

php只是做为Apache的模块应用,因此不须要启动。

PHP7.1

[root@aminglinux-01 php-7.1.6]# ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif

make && make install

模块:ls /usr/local/apache2.4/modules/libphp7.so

同拷贝配置文件:cp php.ini-production /usr/local/php/etc/php.ini


配置httpd支持php

  • httpd主配置文件/usr/local/apache2.4/conf/httpd.conf

打开它,这里面有4个地方须要修改。

  • 第一个ServerName须要修改,

# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80

#

改为以下,否则启动会提示有警告

# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80

#

改完如发现如下错误,系统安装了php5,和7 把php7注释掉就恢复。

[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::dbd:48aa:6994:bf39. Set the 'ServerName' directive globally to suppress this message
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79: 127106 段错误               $HTTPD -k $ARGV
[root@aminglinux-01 ~]#

在运行一次,就不会有提示警告了,若是出现如下错误,说明同时起了两个php,把php7.1注释掉就行了

[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl restart
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79: 127111 段错误               $HTTPD -k $ARGV
[root@aminglinux-01 ~]#

注释:在php.conf 找到php7

LoadModule php5_module        modules/libphp5.so
#LoadModule php7_module        modules/libphp7.so
  • 第二个修改处 Require all denied 修改成granted

<Directory />
    AllowOverride none
    Require all denied
</Directory>

改成:

<Directory />
    AllowOverride none
    Require all granted
</Directory>

修改他的目的是,容许全部请求,若是不设置该行,则咱们访问的时候会报403错误。

  • 第三个修改:再搜索下面这一行:
AddType application/x-gzip .gz .tgz

下面添加一行:

AddType application/x-httpd-php .php

不加不能php解析

第四个更改,接着找到下面这一段:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

将该行修改成:

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

而后

[root@aminglinux-01 httpd-2.4.27]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aminglinux-01 httpd-2.4.27]# /usr/local/apache2.4/bin/apachectl graceful 
[root@aminglinux-01 httpd-2.4.27]#
  • 测试一下是否能正确解析php

建立一个脚本(都是在htdocs目录下)

vi /usr/local/apache2.4/htdocs/1.php

写入一下内容

<?php
phpinfo();
?>
  • 临时开启80端口,否则window浏览器没法访问

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

apache正常启动的状况,用网页是能够

  • graceful 从新加载配置文件

[root@aminglinux-01 httpd-2.4.27]# /usr/local/apache2.4/bin/apachectl graceful 
[root@aminglinux-01 httpd-2.4.27]#
  • -t 检查服务配置文件的语法是否正确

[root@aminglinux-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK

而后访问:http://192.168.245.128/1.php 若是能正常访问,说明php解析正常。

若是没有成功解析,能够-M查看有没有 加载php模块。


扩展

相关文章
相关标签/搜索