首页 > 代码库 > Linux第十二周

Linux第十二周

1、请描述一次完整的http请求处理过程;

1、建立TCP连接

2、Web浏览器向Web服务器发送请求

3、Web浏览器发送请求头信息

建立连接后,客户机发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号,后边是MIME信息包括请求修饰符、客户机信息和可能的内容

4、Web服务器应答

服务器接到请求后,给予相应的响应信息,其格式为一个状态行,包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。

5、Web服务器发送应答头信息

6、Web服务器向浏览器发送数据

7、Web服务器关闭TCP连接

2、httpd所支持的处理模型有哪些,他们的分别使用于哪些环境。

httpd所支持的事务处理模型主要有:

prefork

worker

event

他们分别使用于以下场景:

prefork:多进程模型,每个进程负责响应一个请求。prefork模型在工作时,由一个主进程负责生成n个子进程,即工作进程。每个工作进程响应一个用户请求,即使当前没有用户请求,它亦会预先生成多个空闲进程,随时等待请求连接,这样的好处是,服务器不用等到请求到达时,才去临时建立进程,缩短了进程创建的时间。提高连接效率。但受限于linux的特性,工作进程数上限为1024个,如果超出该数量,服务器性能会急剧降低。因而,prefork模型的最大并发连接数量为1024.由于每个工作进程相对独立,就算崩溃了,也不会对其它进程有明显影响。所以,该模型的特点是稳定可靠,适合于并发量适中而又追求稳定的用户使用。

worker:多线程模型,每个线程响应一个请求。worker模型在工作时,主进程负责生成多个子进程,同时每个子进程负责生成多个线程,每个线程响应一个用户请求。同时,worker模型也会预先创建一些空闲线程来等待用户连接。并发连接数,如果生成进程数为m,线程数为n,则并发数可达到m*n个。但由于在linux中,原生不支持线程,且进程本身就足够轻量化,与线程的区别不是很大,因而,worker模型在linux环境中的实际性能表现与prefork相差无几。

event:事件驱动模型,每个线程响应n个用户请求。event模型工作时,由主进程生成m个子进程,每个单独的子进程可响应n个用户请求。因而,event的并发数量可达到m*n个,同时,因为event的子进程为一对多,节省大量CPU进程切换上下文的时间,也没有了linux系统的1024个进程限制,所以,event模型是三种模型中效率最高的一种。可以突破10K的限制(即并发数1W),对海量的系统特别适用。

3、源码编译安装LAMP环境(基于wordpress程序),并写出详细的安装、配置、测试过程。

以CentOS7.2,http2.4.16,mysql-5.6.26.tar.gz,mysql-5.6.26.tar.gz为例,源码编译安装LAMP,详细步骤如下:

安装开发环境

[root@localhost ~]# yum groupinstall "Development Tools" "Server Platform Development" -y


1、编译安装Apache

解决依赖关系

(1)编译安装apr

[root@localhost ~]# tar xf apr-1.5.2.tar.gz 

[root@localhost ~]# cd apr-1.5.2/

[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr

root@localhost ~]# make && make install && cd

(2)编译安装apr-util

[root@localhost ~]# tar xf apr-util-1.5.4.tar.bz2 

[root@localhost ~]# cd apr-util-1.5.4/

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

[root@localhost apr-util-1.5.4]# make && make install && cd

(3)httpd-2.4.16依赖于pcre-devel软件包

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


编译安装httpd-2.4.16

[root@localhost ~]# tar xf httpd-2.4.16.tar.bz2 

[root@localhost ~]# cd httpd-2.4.16/

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

[root@localhost ~]# make -j 4 && make install &&cd

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

添加

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

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


#编辑apache服务脚本

[root@localhost ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd

[root@localhost ~]# vim /etc/init.d/httpd

添加

# chkconfig: 2345 85 15

# description: httpd startup for the Apache Http Server

[root@localhost ~]# service httpd start

[root@localhost ~]# chkconfig --add httpd

[root@localhost ~]# chkconfig httpd on

[root@localhost ~]# ss -tnl | grep :80

LISTEN     0      128         :::80                      :::* 


2、编译安装MySQL


#添加mysql用户

[root@localhost ~]# groupadd -g 316 mysql

[root@localhost ~]# useradd -g mysql -u 316 -r -s /sbin/nologin mysql

#安装mysql依赖的软件包

[root@localhost ~]# yum -y install cmake ncurses-devel

#编译安装mysql 

[root@localhost ~]# tar xf mysql-5.6.26.tar.gz -C /usr/local/

[root@localhost ~]# cd /usr/local/mysql-5.6.26/

[root@localhost mysql-5.6.26]# cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci  -DWITH_EXTRA_CHARSETS=all  -DWITH_MYISAM_STORAGE_ENGINE=1  -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_MEMORY_STORAGE_ENGINE=1  -DWITH_READLINE=1  -DENABLED_LOCAL_INFILE=1  -DMYSQL_DATADIR=/usr/local/mysql/data  -DMYSQL-USER=mysql

[root@localhost mysql-5.6.26]# make -j 4 && make install

[root@localhost mysql-5.6.26]# cd && chown -R mysql:mysql /usr/local/mysql/

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# sed  -i ‘s#^basedir=#basedir=/usr/local/mysql#‘ /etc/init.d/mysqld

[root@localhost ~]# sed  -i ‘s#^datadir=#datadir=/usr/local/mysql/data#‘ /etc/init.d/mysqld

[root@localhost ~]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

[root@localhost ~]# sed -i ‘/^\[mysqld\]/adatadir = /usr/local/mysql/data‘ /etc/my.cnf

[root@localhost ~]# sed -i ‘/^\[mysqld\]/abasedir = /usr/local/mysql‘ /etc/my.cnf

[root@localhost ~]# chkconfig mysqld on

[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf  --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

[root@localhost ~]# ln -s /usr/local/mysql/bin/* /bin/

[root@localhost ~]# service mysqld start


3、安装PHP

#安装PHP依赖的软件包

[root@localhost ~]# yum -y install libxml2-devel


#编译安装PHP

[root@localhost ~]# tar xf php-5.6.13.tar.bz2 

[root@localhost ~]# cd php-5.6.13/

[root@localhost php-5.6.13]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/usr/local/php

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

[root@localhost php-5.6.13]# cp php.ini-production /usr/local/php/php.ini;cd

[root@localhost ~]# sed -i ‘s/index.html/index.html index.php/‘ /etc/httpd/httpd.conf

[root@localhost ~]#  sed -i ‘377a    AddType application/x-httpd-php .php‘ /etc/httpd/httpd.conf 

[root@localhost ~]# sed -i ‘378a    AddType application/x-httpd-php-source .phps‘ /etc/httpd/httpd.conf 

[root@localhost ~]# service httpd  restart

[root@localhost ~]# echo ‘<?php phpinfo();  ?>‘ >/usr/local/apache/htdocs/index.php

4、wordpress程序的安装

#下载wordpress程序

[root@localhost ~]# wget https://cn.wordpress.org/wordpress-4.5.3-zh_CN.tar.gz

#解压wordpress到/usr/local/apache/htdocs/

[root@localhost ~]# tar xf wordpress-4.5.3-zh_CN.tar.gz -C /usr/local/apache/htdocs/

[root@localhost ~]# cd /usr/local/apache/htdocs/

[root@localhost htdocs]# chown root:root wordpress/ -R

[root@localhost htdocs]# cd wordpress/

[root@localhost wordpress]# cp wp-config-sample.php wp-config.php

(1)修改数据库名

修改 

define(‘DB_NAME‘, ‘database_name_here‘);

define(‘DB_NAME‘, ‘wordpress‘);

(2) 修改MySQL数据库登录用户名

修改 

define(‘DB_USER‘, ‘username_here‘);

define(‘DB_USER‘, ‘root‘);

(3)修改MySQL数据库登录密码

修改

define(‘DB_PASSWORD‘, ‘password_here‘);

define(‘DB_PASSWORD‘, ‘magedu‘);

#登录mysql创建wordpress数据库,并设置root用户登录密码

[root@localhost ~]# mysql

mysql> create database wordpress;

mysql> use mysql;

mysql> update user set password=PASSWORD(‘magedu‘) where user=‘root‘;

mysql> flush privileges;

mysql> \q

在浏览器中输入

http://192.168.1.72/wordpress/


设置注册用户的用户名和密码,完成安装wordPress

4、建立httpd服务器(基于编译的方式进行),要求:

     提供两个基于名称的虚拟主机:

    (a)www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;

    (b)www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;

    (c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;

    (d)通过www1.stuX.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);


安装开发环境

[root@localhost ~]# yum groupinstall "Development Tools" "Server Platform Development" -y


1、编译安装Apache

解决依赖关系

(1)编译安装apr

[root@localhost ~]# tar xf apr-1.5.2.tar.gz 

[root@localhost ~]# cd apr-1.5.2/

[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr

root@localhost ~]# make && make install && cd

(2)编译安装apr-util

[root@localhost ~]# tar xf apr-util-1.5.4.tar.bz2 

[root@localhost ~]# cd apr-util-1.5.4/

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

[root@localhost apr-util-1.5.4]# make && make install && cd

(3)httpd-2.4.16依赖于pcre-devel软件包

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


编译安装httpd-2.4.16

[root@localhost ~]# tar xf httpd-2.4.16.tar.bz2 

[root@localhost ~]# cd httpd-2.4.16/

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

[root@localhost ~]# make -j 4 && make install &&cd

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

添加

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

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


#编辑apache服务脚本

[root@localhost ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd

[root@localhost ~]# vim /etc/init.d/httpd

添加

# chkconfig: 2345 85 15

# description: httpd startup for the Apache Http Server

[root@localhost ~]# service httpd start

[root@localhost ~]# chkconfig --add httpd

[root@localhost ~]# chkconfig httpd on

[root@localhost ~]# ss -tnl | grep :80

LISTEN     0      128         :::80                      :::* 


2、编辑apache配置文件

[root@localhost ~]# cd /etc/httpd/

[root@localhost httpd]# cp httpd.conf{,.bak}

[root@localhost httpd]# vim httpd.conf

(1)启用虚拟主机

修改

#Include /etc/httpd/extra/httpd-vhosts.conf

Include /etc/httpd/extra/httpd-vhosts.conf

(2)添加两个虚拟主机目录的访问权限

在末尾添加以下内容:

<Directory "/web/vhosts/www1">

options none

allowoverride none

Require all granted

</Directory>


<Directory "/web/vhosts/www2">

options none

allowoverride none

Require all granted

</Directory>


(3)创建虚拟主机目录

[root@localhost ~]# mkdir -p /web/vhosts/www{1,2}

[root@localhost ~]# mkdir /var/log/httpd

[root@localhost ~]# touch /var/log/httpd/www{1,2}.{err,access}



(4)编辑虚拟主机文件

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf

在末尾添加以下内容

<VirtualHost *:80>

    DocumentRoot "/web/vhosts/www1"

    ServerName www1.stuX.com

    ErrorLog "/var/log/httpd/www1.err"

    CustomLog "/var/log/httpd/www1.access" common

</VirtualHost>


<VirtualHost *:80>

    DocumentRoot "/web/vhosts/www2"

    ServerName www2.stuX.com

    ErrorLog "/var/log/httpd/www2.err"

    CustomLog "/var/log/httpd/www2.access" common

</VirtualHost>

[root@localhost ~]# service httpd restart


(5)创建虚拟主机主页文件

[root@localhost ~]# echo "<h1>www1.stuX.com</h1>" > /web/vhosts/www1/index.html

[root@localhost ~]# echo "<h1>www2.stuX.com</h1>" > /web/vhosts/www2/index.html 


(6)客户端测试,需要在DNS服务器或hosts中配置好虚拟主机

[root@localhost ~]# curl www1.stuX.com

<h1>www1.stuX.com</h1>

[root@localhost ~]# curl www2.stuX.com 

<h1>www2.stuX.com</h1>


3、构建Server-Status设置

在www1.stuX.com里,增加server-status的设置

[root@www1 httpd]# vim /etc/httpd/extra/httpd-vhosts.conf 

修改www1.stuX.com主机的配置文件为

<VirtualHost *:80>

DocumentRoot "/web/vhosts/www1"

ServerName www1.stuX.com

ErrorLog "/var/log/httpd/www1.err"

CustomLog "/var/log/httpd/www1.access" common

<Location /server-status>

SetHandler server-status

AuthType Basic

AuthName "Server-Status"

AuthUserFile "/etc/httpd/extra/.htpasswd"

Require valid-user

</location>

</VirtualHost>


#生成密码文件

[root@localhost ~]# htpasswd -cm /etc/httpd/extra/.htpasswd status #密码也设为status

[root@www1 httpd]# service httpd restart

5、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;

   (1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu);

   (2)设置部门为Ops,主机名为www2.stuX.com,邮件为admin@stuX.com;

#CA与Web在同一主机上

1、创建私有CA

(1)创建所需要的文件

[root@www1 ~]# cd /etc/pki/CA

[root@www1 CA]# touch index.txt

[root@www1 CA]# echo 01 > serial


(2)CA自签证书

[root@www1 CA]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)

Generating RSA private key, 2048 bit long modulus

...............................................+++

...........................+++

e is 65537 (0x10001)

[root@www1 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pemYou are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter ‘.‘, the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [Default Company Ltd]:MageEdu 

Organizational Unit Name (eg, section) []:OPS

Common Name (eg, your name or your server‘s hostname) []:ca.stuX.com

Email Address []:ca@stuX.com

(3)发证

[root@www1 CA]# cd /etc/httpd/

[root@www1 httpd]# mkdir ssl

[root@www1 httpd]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)Generating RSA private key, 2048 bit long modulus

.......................................+++

.............................................................................................................................................................................+++

e is 65537 (0x10001)

[root@www1 httpd]# openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.csrYou are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter ‘.‘, the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [Default Company Ltd]:MageEdu

Organizational Unit Name (eg, section) []:Ops

Common Name (eg, your name or your server‘s hostname) []:www2.stuX.com

Email Address []:admin@stuX.com


Please enter the following ‘extra‘ attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

 

  [root@www1 httpd]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365Using configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

        Serial Number: 1 (0x1)

        Validity

            Not Before: Oct 19 09:09:37 2016 GMT

            Not After : Oct 19 09:09:37 2017 GMT

        Subject:

            countryName               = CN

            stateOrProvinceName       = HA

            organizationName          = MageEdu

            organizationalUnitName    = Ops

            commonName                = www2.stuX.com

            emailAddress              = admin@stuX.com

        X509v3 extensions:

            X509v3 Basic Constraints: 

                CA:FALSE

            Netscape Comment: 

                OpenSSL Generated Certificate

            X509v3 Subject Key Identifier: 

                CE:2A:51:9E:2C:5E:05:B4:79:AB:14:C8:32:E7:68:42:2B:E8:CD:4E

            X509v3 Authority Key Identifier: 

                keyid:88:96:ED:8A:43:0F:8B:2A:DD:D4:E5:B1:02:7A:6C:9F:11:45:FF:E9


Certificate is to be certified until Oct 19 09:09:37 2017 GMT (365 days)

Sign the certificate? [y/n]:y



1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

[root@www1 httpd]# cp /etc/pki/CA/certs/httpd.crt /etc/httpd/ssl/

[root@www1 httpd]# ls /etc/httpd/ssl/

httpd.crt  httpd.csr  httpd.key

[root@www1 ~]# yum -y install mod_ssl

[root@www1 ~]# vim /etc/httpd/conf.d/ssl.conf

在末尾添加以下内容

<VirtualHost 192.168.1.73:443>

DocumentRoot /web/vhosts/www2/

ServerName  www2.stuX.com:443

SSLEngine on

SSLCertificateFile /etc/httpd/ssl/httpd.crt

SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

</VirtualHost>

6、在LAMP架构中,请分别以php编译成httpd模块形式和php以fpm工作为独立守护进程的方式来支持httpd,列出详细的过程。

安装开发环境

[root@localhost ~]# yum groupinstall "Development Tools" "Server Platform Development" -y


1、编译安装Apache

解决依赖关系

(1)编译安装apr、apr-util

[root@localhost ~]# tar xf apr-1.5.2.tar.gz 

[root@localhost ~]# cd apr-1.5.2/

[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr

root@localhost ~]# make && make install && cd


[root@localhost ~]# tar xf apr-util-1.5.4.tar.bz2 

[root@localhost ~]# cd apr-util-1.5.4/

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

[root@localhost apr-util-1.5.4]# make && make install && cd

(2)httpd-2.4.16依赖于pcre-devel软件包

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


编译安装httpd-2.4.16

[root@localhost ~]# tar xf httpd-2.4.16.tar.bz2 

[root@localhost ~]# cd httpd-2.4.16/

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

[root@localhost ~]# make -j 4 && make install && cd

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

添加

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

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


#编辑apache服务脚本

[root@localhost ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd

[root@localhost ~]# vim /etc/init.d/httpd

添加

# chkconfig: 2345 85 15

# description: httpd startup for the Apache Http Server

[root@localhost ~]# service httpd start

[root@localhost ~]# chkconfig --add httpd

[root@localhost ~]# chkconfig httpd on

[root@localhost ~]# ss -tnl | grep :80

LISTEN     0      128         :::80                      :::* 


2、编译安装MySQL


#添加mysql用户

[root@localhost ~]# groupadd -g 316 mysql

[root@localhost ~]# useradd -g mysql -u 316 -r -s /sbin/nologin mysql

#安装mysql依赖的软件包

[root@localhost ~]# yum -y install cmake ncurses-devel

#编译安装mysql 

[root@localhost ~]# tar xf mysql-5.6.26.tar.gz -C /usr/local/

[root@localhost ~]# cd /usr/local/mysql-5.6.26/

[root@localhost mysql-5.6.26]# cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci  -DWITH_EXTRA_CHARSETS=all  -DWITH_MYISAM_STORAGE_ENGINE=1  -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_MEMORY_STORAGE_ENGINE=1  -DWITH_READLINE=1  -DENABLED_LOCAL_INFILE=1  -DMYSQL_DATADIR=/usr/local/mysql/data  -DMYSQL-USER=mysql

[root@localhost mysql-5.6.26]# make -j 4 && make install

[root@localhost mysql-5.6.26]# cd && chown -R mysql:mysql /usr/local/mysql/

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# sed  -i ‘s#^basedir=#basedir=/usr/local/mysql#‘ /etc/init.d/mysqld

[root@localhost ~]# sed  -i ‘s#^datadir=#datadir=/usr/local/mysql/data#‘ /etc/init.d/mysqld

[root@localhost ~]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

[root@localhost ~]# sed -i ‘/^\[mysqld\]/adatadir = /usr/local/mysql/data‘ /etc/my.cnf

[root@localhost ~]# sed -i ‘/^\[mysqld\]/abasedir = /usr/local/mysql‘ /etc/my.cnf

[root@localhost ~]# chkconfig mysqld on

[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf  --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

[root@localhost ~]# ln -s /usr/local/mysql/bin/* /bin/

[root@localhost ~]# service mysqld start


3、安装PHP

安装php依赖的软件

[root@localhost ~]# yum -y install libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel

[root@localhost ~]# tar xf libmcrypt-2.5.8.tar.bz2 

[root@localhost ~]# cd libmcrypt-2.5.8/

[root@localhost libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt

[root@localhost libmcrypt-2.5.8]# make && make install

[root@localhost libmcrypt-2.5.8]# cd

(一)httpd模块形式编译安装PHP

[root@localhost ~]# tar xf php-5.6.13.tar.bz2 

[root@localhost ~]# cd php-5.6.13/

#以httpd模块方式运行,所以需要在编译时指定apache的apxs2的目录路径 --with-apxs2=/usr/local/apache/bin/apxs 

[root@localhost php-5.6.13]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/lib64 --enable-xml  --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt=/usr/local/libcrympt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts

[root@localhost php-5.6.13]# make -j 4 && make install

[root@localhost php-5.6.13]# cp php.ini-production /etc/php.ini && cd

#编辑apache配置文件

[root@localhost ~]#sed -i ‘s/index.html/index.html index.php/‘ /etc/httpd/httpd.conf

[root@localhost ~]#sed -i ‘377a    AddType application/x-httpd-php .php‘ /etc/httpd/httpd.conf

[root@localhost ~]#sed -i ‘378a    AddType application/x-httpd-php-source .phps‘ /etc/httpd/httpd.conf

[root@localhost ~]#service httpd  restart

[root@localhost ~]#echo ‘<?php phpinfo();  ?>‘ >/usr/local/apache/htdocs/index.php

访问http://192.168.1.72/index.php进行测试


(二)以fpm模式运行

[root@localhost ~]# sed -i ‘1a/usr/local/libmcrypt/lib‘ /etc/ld.so.conf

[root@localhost ~]# sed -i ‘2a/usr/local/mysql/lib‘ /etc/ld.so.conf

[root@localhost ~]# yum install php-pear -y

[root@localhost ~]# ldconfig

[root@localhost ~]# echo ‘ldconfig‘ >> /etc/rc.local

[root@localhost ~]# tar xf php-5.6.13.tar.bz2 

[root@localhost ~]# cd php-5.6.13/

#以fpm模式运行,使能fpm选项,--enable-fpm, --with-apxs2一项就不需要启用了

[root@localhost php-5.6.13]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt

[root@localhost php-5.6.13]# make -j 4 && make install

[[root@localhost php-5.6.13]# cp php.ini-production /usr/local/php/php.ini && cd

[root@localhost php-5.6.13]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@localhost php-5.6.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@localhost php-5.6.13]#  chmod +x /etc/init.d/php-fpm

[root@localhost php-5.6.13]# chkconfig php-fpm on

[root@localhost php-5.6.13]# /etc/init.d/php-fpm start

Starting php-fpm  done

[root@localhost php-5.6.13]# netstat -antup | grep php-fpm

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9769/php-fpm: maste 


#编辑apache配置文件

[root@localhost ~]# sed -i ‘s/^#LoadModule proxy_fcgi/LoadModule proxy_fcgi/‘ /etc/httpd/httpd.conf

[root@localhost ~]# sed -i ‘s/^#LoadModule proxy_module/LoadModule proxy_module/‘ /etc/httpd/httpd.conf

[root@localhost ~]#sed -i ‘s/index.html/index.html index.php/‘ /etc/httpd/httpd.conf

[root@localhost ~]#sed -i ‘377a    AddType application/x-httpd-php .php‘ /etc/httpd/httpd.conf

[root@localhost ~]#sed -i ‘378a    AddType application/x-httpd-php-source .phps‘ /etc/httpd/httpd.conf

[root@localhost ~]#service httpd  restart

[root@localhost ~]#echo ‘<?php phpinfo();  ?>‘ >/usr/local/apache/htdocs/index.php

访问http://192.168.1.72/index.php进行测试


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1866104

Linux第十二周