天天看點

mysql資料庫,apacha伺服器,php伺服器的搭建

部落格

一、安裝mysql:

準備壓縮包:mysql-5.5.33-linux2.6-x86_64.tar.gz

[root@www ~]# groupadd -r mysql<=========================建立一個mysql組,因為mysql的運作是要靠mysql使用者的

[root@www ~]# useradd -g mysql -r -s /sbin/nologin mysql<==========建立mysql使用者

[root@www ~]# id mysql<==================================确認一下是否建好了

uid=496(mysql) gid=493(mysql) groups=493(mysql)

[root@www ~]# ls

anaconda-ks.cfg  install.log  install.log.syslog  mysql-5.5.33-linux2.6-x86_64.tar.gz

[root@www ~]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local<======将壓縮包解壓

[root@www ~]# cd /usr/local<===========================切換到此目錄下,檢視一下生成的檔案

[root@www local]# ls

bin  etc  games  include  lib  lib64  libexec  mysql-5.5.33-linux2.6-x86_64  sbin  share  src

[root@www local]# ln -sv mysql-5.5.33-linux2.6-x86_64/ mysql<=======為檔案建立一個連結,                                                                        以後使用時更友善

`mysql' -> `mysql-5.5.33-linux2.6-x86_64/'

[root@www local]# mkdir /data<=======================建立data目錄,為下面裝置挂載時使用

[root@www local]# fdisk /dev/sda<========================建立一個分區

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

        switch off the mode (command 'c') and change display units to

        sectors (command 'u').

Command (m for help): n

Command action

  e   extended

  p   primary partition (1-4)

p

Partition number (1-4): 3

First cylinder (7859-15665, default 7859):

Using default value 7859

Last cylinder, +cylinders or +size{K,M,G} (7859-15665, default 15665): +20G

Command (m for help): t

Hex code (type L to list codes): 8e

Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

[root@www local]# cat /proc/partitions<=======檢視一下分區狀況,如果建立的分區沒有顯示出                                                                           來,就重新開機一下

major minor  #blocks  name

  8        0  125829120 sda

  8        1     204800 sda1

  8        2   62914560 sda2

  8        3   20979891 sda3<==================這是建立的分區

253        0   20971520 dm-0

253        1    2097152 dm-1

253        2   10485760 dm-2

253        3   20971520 dm-3

[root@www local]# pvcreate /dev/sda3

 Physical volume "/dev/sda3" successfully created

 [root@www local]# vgcreate myvg /dev/sda3

 Volume group "myvg" successfully created

[root@www local]# lvcreate -L 10G -n mydata myvg

 Logical volume "mydata" created

[root@www local]# lvs<===============檢視一下建立的卷組資訊

 LV     VG   Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert

 mydata myvg -wi-a---- 10.00g                                            

 root   vg0  -wi-ao--- 20.00g                                            

 swap   vg0  -wi-ao---  2.00g                                            

 usr    vg0  -wi-ao--- 10.00g                                            

 var    vg0  -wi-ao--- 20.00g      

 [root@www local]# mke2fs -t ext4 /dev/myvg/mydata<===========格式化卷組

[root@www local]# vim /etc/fstab<=================開機挂載

 # Created by anaconda on Sat Mar 22 10:51:06 2014

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/vg0-root    /                       ext4    defaults        1 1

UUID=21013d46-de12-479a-beed-1a04aa11033d /boot                   ext4    defaults        1 2

/dev/mapper/vg0-usr     /usr                    ext4    defaults        1 2

/dev/mapper/vg0-var     /var                    ext4    defaults        1 2

/dev/mapper/vg0-swap    swap                    swap    defaults        0 0

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

/dev/myvg/mydata       /data                    ext4    defaults        0 0<==這是要挂載的卷組

~                                                                            

[root@www local]# mount -a<=============重載一下,把卷組挂載上來

[root@www local]# mount<=====================檢視一下挂載的卷組資訊

/dev/mapper/vg0-root on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/mapper/vg0-usr on /usr type ext4 (rw)

/dev/mapper/vg0-var on /var type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

/dev/mapper/myvg-mydata on /data type ext4 (rw) <=========這裡就是新挂載的卷組資訊      

[root@www local]# cd /data

[root@www data]# mkdir mydata<======================建立mydata目錄

[root@www data]# chown -R mysql.mysql mydata/<======修改目錄的屬主屬組

[root@www data]# ll<================================确認一下修改的資訊

total 20

drwx------ 2 root  root  16384 Mar 22 12:01 lost+found

drwxr-xr-x 2 mysql mysql  4096 Mar 22 12:06 mydata

[root@www data]# cd /usr/local/mysql

[root@www mysql]# chown -R root.mysql ./*<===========修改mysql檔案的屬主及屬組

[root@www mysql]# ll<================================确認更改成功

total 200

drwxr-xr-x  2 root mysql   4096 Mar 22 11:54 bin

-rw-r--r--  1 root mysql  17987 Jul 15  2013 COPYING

drwxr-xr-x  3 root mysql   4096 Mar 22 11:54 data

drwxr-xr-x  2 root mysql   4096 Mar 22 11:54 docs

drwxr-xr-x  3 root mysql   4096 Mar 22 11:54 include

-rw-r--r--  1 root mysql 134493 Jul 15  2013 INSTALL-BINARY

drwxr-xr-x  3 root mysql   4096 Mar 22 11:54 lib

drwxr-xr-x  4 root mysql   4096 Mar 22 11:54 man

drwxr-xr-x 10 root mysql   4096 Mar 22 11:54 mysql-test

-rw-r--r--  1 root mysql   2496 Jul 15  2013 README

drwxr-xr-x  2 root mysql   4096 Mar 22 11:54 scripts

drwxr-xr-x 27 root mysql   4096 Mar 22 11:54 share

drwxr-xr-x  4 root mysql   4096 Mar 22 11:54 sql-bench

drwxr-xr-x  3 root mysql   4096 Mar 22 11:54 support-files

[root@www mysql]# scripts/mysql_install_db --datadir=/data/mydata --user=mysql<==安裝并初始化mysql,這是安裝之前很重要的一步

Installing MySQL system tables...

OK

Filling help tables...

[root@www mysql]# ls /data/mydata

mysql  performance_schema  test

[root@www mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld<=====提供服務腳本

[root@www mysql]# chkconfig --add mysqld<==========================開機自動啟動此程序

[root@www mysql]# chkconfig --list mysqld

mysqld         0:off1:off2:on3:on4:on5:on6:off

[root@www mysql]# cp support-files/my-large.cnf /etc/my.cnf<===========提供配置檔案,注意這裡是把原先的檔案覆寫了

cp: overwrite `/etc/my.cnf'? y

[root@www mysql]# vim /etc/my.cnf<=============編輯配置檔案

# The MySQL server

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

# Try number of CPU's*2 for thread_concurrency

thread_concurrency 4<====================================大概在40行左右,這裡更改一下

datadir = /data/mydata<==================================要加下此資訊,指定mysql的目錄

[root@www mysql]# service mysqld start<============================啟動mysql驗證一下是否能夠正常啟動

Starting MySQL...                                          [  OK  ]

[root@www mysql]# vim /etc/profile.d/mysql.sh<============為mysql導入指令檔案

export PATH=/usr/local/mysql/bin:$PATH

~                                        

[root@www mysql]# cd

[root@www ~]# . /etc/profile.d/mysql.sh<================讓系統執行一下剛才建立的檔案

現在mysql就可能運作起來了,我們進入mysql看一下吧,看看還有什麼要設定的

[root@www ~]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> use mysql<===============================建立一個使用者表

Database changed

mysql> SELECT user,host,password from user;<=====================

+------+---------------+----------+

| user | host          | password |

| root | localhost     |          |

| root | www.jiayu.com |          |

| root | 127.0.0.1     |          |

| root | ::1           |          |

|      | localhost     |          |

|      | www.jiayu.com |          |

6 rows in set (0.01 sec)

mysql> DROP USER ''@'www.jiayu.com' ;<==============================

Query OK, 0 rows affected (0.00 sec)

mysql> DROP USER ''@'localhost';<=================================

mysql> UPDATE user SET password=PASSWORD('love') WHERE user='root';<============建立密碼

Query OK, 4 rows affected (0.02 sec)

Rows matched: 4  Changed: 4  Warnings: 0

mysql> SELECT user,host,password from user;<=========================檢視密碼是否生成

+------+---------------+-------------------------------------------+

| user | host          | password                                  |

| root | localhost     | *656B19382E8A16676406422882380722D756E17A |

| root | www.jiayu.com | *656B19382E8A16676406422882380722D756E17A |

| root | 127.0.0.1     | *656B19382E8A16676406422882380722D756E17A |

| root | ::1           | *656B19382E8A16676406422882380722D756E17A |

4 rows in set (0.00 sec)

mysql> FLUSH PRIVILEGES;《===========================================

[root@www ~]# mysql -uroot -hlocalhost -plove <==========密碼建立之後,要這樣才能進入mysql

Welcome to the MySQL monitor.  Commands end with ; or

[root@www ~]# vim .my.cnf<============如果上面進入系統太麻煩了,那就他建一個隐藏檔案吧,把密碼儲存到裡面

[mysql]

user=root

host=localhost

password=love

~            

[root@www ~]# mysql<========================================再進入mysql就不用輸密碼了

Your MySQL connection id is 3

[root@www ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql<<===================導出頭檔案

`/usr/include/mysql' -> `/usr/local/mysql/include/'

[root@www ~]# vim /etc/ld.so.conf.d/mysql.conf<===================================導出庫檔案

/usr/local/mysql/lib

[root@www ~]# ldconfig

[root@www ~]# ldconfig -p | grep mysql

libtcmalloc_minimal.so.0 (libc6,x86-64) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0

libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16

libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18<====關鍵要看到這一項

libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.16

libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so

二、安裝httpd:      httpd的安裝要依靠apr和apr-util是以安裝之前必須先安裝這兩個檔案。

httpd-2.4.9.tar.bz2

apr-1.5.0.tar.bz2

apr-util-1.5.3.tar.bz2

[root@www ~]# yum install pcre-devel -y

[root@www ~]# tar xf apr-1.5.0.tar.bz2

[root@www ~]# cd apr-1.5.0

[root@www apr-1.5.0]# ./configure --prefix=/usr/local/apr

[root@www apr-1.5.0]# make && make install

[root@www ~]# tar xf apr-util-1.5.3.tar.bz2

[root@www ~]# cd apr-util-1.5.3

[root@www apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

[root@www httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --with-zlib --with-pcre --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event

[root@www httpd-2.4.9]make && make install

--prefix=/usr/local/apache<===========指定安裝路徑

--sysconfdir=/etc/httpd24<============配置檔案的存放位置

--enable-so --enable-ssl --enable-cgi <====開啟so,ssl,cgi功能

--enable-rewrite<======================開啟字型設定功能

--with-apr=/usr/local/apr<=============安裝時到哪去找apr

--with-apr-util=/usr/local/apr-util/<==安裝時到哪去找apr-util

--enable-mpms-shared=all <=========編輯mpms功能

--with-mpm=event<==================指定php用event模式

[root@www httpd-2.4.9]# cd /etc/rc.d/init.d

[root@www init.d]# cp httpd httpd24

[root@www init.d]# vim httpd24

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl<====在約在40行左右,把原來的路徑改成這樣

httpd=${HTTPD-/usr/local/apache/bin/httpd}<===指定新的路徑

[root@www init.d]# chkconfig --add httpd24

[root@www init.d]# chkconfig --list httpd24

httpd24        0:off1:off2:off3:off4:off5:off6:off

[root@www ~]# vim /etc/profile.d/httpd.sh

export PATH=/usr/local/apache/bin:$PATH

[root@www ~]# . /etc/profile.d/httpd.sh

[root@www ~]# vim /etc/httpd24/httpd.conf

# Do not add a slash at the end of the directory path.  If you point

# ServerRoot at a non-local disk, be sure to specify a local disk on the

# Mutex directive, if file-based mutexes are used.  If you wish to share the

# same ServerRoot for multiple httpd daemons, you will need to change at

# least PidFile.

ServerRoot "/usr/local/apache"

pidFile "var/run/httpd/httpd.conf"<=====大約40行左右,把這個pidFile加上

[root@www ~]# service httpd24 start

Starting httpd:                                            [  OK  ]

安裝php伺服器

要準備的檔案

php-5.4.26.tar.bz2

xcache-3.0.3.tar.bz2

phpMyAdmin-4.0.5-all-languages.zip

[root@www ~]# tar xf php-5.4.26.tar.bz2

[root@www ~]# cd php-5.4.26

[root@www php-5.4.26]#./configure --prefix=/usr/local/php

--with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl

[root@www php-5.4.26]# make && make install

[root@www php-5.4.26]# cp php.ini-production /etc/php.ini

編輯配置檔案

[root@www php-5.4.26]# vim /etc/httpd24/httpd.conf

  在382行左右AddType下面如下兩行

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

在252行左右修改成如下

<IfModule dir_module>

   DirectoryIndex index.php index.html

</IfModule>

root@www ~]# service httpd24 reload

Reloading httpd:

[root@www ~]# ss -tnlp

下面是測試php是不是工作正常了

[root@www ~]# cd /usr/local/apache

[root@www apache]# ls

bin  build  cgi-bin  error  htdocs  icons  include  logs  man  manual  modules

[root@www apache]# cd htdocs

[root@www htdocs]# ls

index.html

[root@www htdocs]# mv index.html index.php

index.php

[root@www htdocs]# vim index.php

<? php

    phpinfo();

?>

~   在浏覽器裡打開你的伺服器位址,看看是否運作正常了