unijun 发表于 2015-11-15 14:00:03

用源代码搭建LAMP环境

  1:LAMP环境的搭建方法:
  第一种:RPM包安装方式
  第二种:源代码包编译安装方式
  https:443
http:80
  客户端------WEB服务器------MYSQL数据库
当客户端发送请求时WEB服务器通过PHP连接后台MYSQL数据库
同样:perl/java/python都可以用来连接后台的数据库
MYSQL有专门自己的存储集群
  MYSQL的三种架构:单机/AB复制/集群
  相关软件包:
# rpm -qa | grep mysql
mysql-5.0.45-7.el5    客户端应用程序
mysql-server-5.0.45-7.el5   服务器端的应用程序
php-mysql-5.1.6-23.el5    php用来连接数据库的工具
  当php被apache编译成模块时:
# ls /etc/httpd/modules/libphp5.so
/etc/httpd/modules/libphp5.so
  如果是采用rpm包安装的话,php有自己的配置文件:
# pwd
/etc/httpd/conf.d
# ls
php.confproxy_ajp.confREADMEwelcome.conf
叫php.conf
php是一种编程语言
  MYSQL是一种关系型数据库管理系统(RDBMS)
MYSQL--databases--表--记录
数据库在文件系统里面都是以目录的形式存在的:/var/lib/mysql下(rpm包安装方式的情况下)
且下面的所有的目录都是700,默认情况下,系统中有两个数据库:mysql/test 其中test是用来测试用的,而mysql则是用来存放用户、表定义、索引之类的东东!而下面的mysql.sock是套接字文件,是用来和客户端连接用的,当mysql服务启动时就存在该文件,如果没启动,则没有!!
  采用源码包搭建LAMP环境
相关软件包及文档:
# ls
AdobeReader_chs-7.0.0-2.i386.rpm
Discuz!_6.0.0_SC_GBK.pcpop.zip
httpd-2.2.11.tar.bz2
lamp.txt
mysql
mysql-5.1.34.tar.gz
mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
php-5.2.9.tar.bz2
phpBB
phpMyAdmin-3.1.4-all-languages.tar.bz2
  现装apache
# tar jxvf httpd-2.2.11.tar.bz2 -C /usr/local/src
现解压,进入解压目录
# pwd
/usr/local/src/httpd-2.2.11
# ls
ABOUT_APACHE      CHANGES      include         modules         srclib
acinclude.m4      config.layoutINSTALL         NOTICE            support
Apache.dsw      configure      InstallBin.dspNWGNUmakefile   test
apachenw.mcp.zipconfigure.in   LAYOUT          os                VERSIONING
build             docs         libhttpd.dsp    README
BuildAll.dsp      emacs-style    LICENSE         README.platforms
BuildBin.dsp      httpd.dsp      Makefile.in   ROADMAP
buildconf         httpd.spec   Makefile.win    server
下面的INSTALL文件主要是用来告诉你如何安装的
# vim INSTALL
10      $ ./configure --prefix=PREFIX
11      $ make
12      $ make install
13      $ PREFIX/bin/apachectl start
也可以查看./configure的相关帮助
# ./configure --help
编译前的准备:
./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl
编译:
make
安装:
make install
安装完成:
# pwd
/usr/local/apache2
# ls
bin    cgi-binerror   icons    lib   man   modules
buildconf   htdocsincludelogsmanual
下面的一系列目录
conf是放配置文件
htdocs是放网页的
logs是用来存放日志的
modules是用来存放模块的
# pwd
/usr/local/apache2/bin
# ls
ab            apu-1-configdbmmanage    htcachecleanhtpasswd   logresolve
apachectl   apxs          envvars      htdbm         httpd      rotatelogs
apr-1-configcheckgid      envvars-stdhtdigest      httxt2dbm
apache的启动文件:apachectl
测试文件:ab
  启动服务:
# ./apachectl start
httpd (pid 3163) already running
# /usr/local/apache2/bin/apachectl restart
现在就可以通过浏览器来访问了
因为命令比较长,可以写一个简单的脚本来执行
如果想apache开机就启动,可以把该命令放到/etc/rc.d/rc.local里面
  
  如何开启https的443端口呢?
首先在服务器端产生一对公私钥
产生证书请求:
# mkdir k
# cd k
#
  # openssl req -new -out server.csr
Generating a 1024 bit RSA private key
......++++++
...++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You 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) :CN
State or Province Name (full name) :beijing
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
  Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# ls
privkey.pemserver.csr
生成两个临时文件
  生成私钥
# openssl rsa -in privkey.pem -out server.key
Enter pass phrase for privkey.pem:
writing RSA key
# ls
privkey.pemserver.csrserver.key
  生成证书:
# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365
Signature ok
subject=/C=CN/ST=beijing/L=Newbury/O=My Company Ltd
Getting Private key
# ls
privkey.pemserver.crtserver.csrserver.key
我们想要的两个文件server.crt/server.key都有了,下面则将这两个文件防止不同的位置
在这之前我们得修改一下apache的主配置文件:
# vim /usr/local/apache2/conf/httpd.conf
修改其445行 将其#号注释去掉
445 Include conf/extra/httpd-ssl.conf
保存退出
然后我们来查看一下:server.crt/server.key两个文件的放置位置帮助:
# pwd
/usr/local/apache2/conf/extra
# ls
httpd-autoindex.confhttpd-languages.conf         httpd-ssl.conf
httpd-dav.conf      httpd-manual.conf            httpd-userdir.conf
httpd-default.conf    httpd-mpm.conf               httpd-vhosts.conf
httpd-info.conf       httpd-multilang-errordoc.conf
查看httpd-ssl.conf
# vim httpd-ssl.conf
99 SSLCertificateFile "/usr/local/apache2/conf/server.crt"
107 SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
进入/usr/local/apache2/conf目录下面
cp /root/k/server.key .
cp /root/k/server.crt .
  重新启动apache服务
# /usr/local/apache2/bin/apachectl restart
这样服务器的443端口就打开了
# nmap 192.168.0.158
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2010-01-19 11:34 CST
Interesting ports on 192.168.0.158:
Not shown: 1673 closed ports
PORT   STATE SERVICE
80/tcp   openhttp
111/tcpopenrpcbind
443/tcpopenhttps
640/tcpopenunknown
913/tcpopenunknown
1000/tcp opencadlock
2049/tcp opennfs
Nmap finished: 1 IP address (1 host up) scanned in 13.108 seconds
测试:
https://192.168.0.158
即可访问
  直接把apache2目录删掉就可以把源码包安装的卸载
# ./httpd -M
查看模块
  
  源码安装MYSQL
# tar zxvf mysql-5.1.34.tar.gz -C /usr/local/src
进入解压目录:
# pwd
/usr/local/src/mysql-5.1.34
# ls
aclocal.m4      COPYING             ltmain.sh    server-tools
BUILD         dbug                Makefile   sql
ChangeLog       depcomp             Makefile.amsql-bench
client          Docs                Makefile.insql-common
CMakeLists.txtEXCEPTIONS-CLIENT   man          storage
cmd-line-utilsextra               missing      strings
config          include             mysql-test   support-files
config.guess    install-sh          mysys      tests
config.h.in   INSTALL-SOURCE      netware      unittest
config.log      INSTALL-WIN-SOURCEplugin       vio
config.status   libmysql            pstack       win
config.sub      libmysqld         README       ylwrap
configure       libmysql_r          regex      zlib
configure.in    libtool             scripts
编译前的准备:
# ./configure --with-mysqld-user=mysql --prefix=/usr/local/mysql --with-extra-charsets=all--exec-prefix=/usr/local/mysql --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-innodb --with-plugins=partition
编译:
make
安装:
make install
  安装完成后的相关目录:
# pwd
/usr/local/mysql
# ls
bindocsincludeliblibexecmysql-testsharesql-bench
# ls
innochecksum       mysql_client_test         mysql_secure_installation
msql2mysql         mysql_config                mysql_setpermission
myisamchk          mysql_convert_table_formatmysqlshow
myisam_ftdump      mysqld_multi                mysqlslap
myisamlog          mysqld_safe               mysqltest
myisampack         mysqldump                   mysql_tzinfo_to_sql
my_print_defaultsmysqldumpslow               mysql_upgrade
mysql            mysql_find_rows             mysql_waitpid
mysqlaccess      mysql_fix_extensions      mysql_zap
mysqladmin         mysql_fix_privilege_tablesperror
mysqlbinlog      mysqlhotcopy                replace
mysqlbug         mysqlimport               resolveip
mysqlcheck         mysql_install_db            resolve_stack_dump
现在我们来初始化MYSQL:
执行./mysql_install_db
默认情况/usr/local/mysql目录下没有var目录,当我们执行MYSQL初始化后在该目录下将会产生var目录
# ./mysql_install_db
Installing MySQL system tables...
100119 13:35:28 Forcing shutdown of 1 plugins
OK
Filling help tables...
100119 13:35:28 Forcing shutdown of 1 plugins
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 MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h stu15.uplooking.com password 'new-password'
Alternatively you can run:
/usr/local/mysql/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 manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/local/mysql/bin/mysqlbug script!
The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/
# cd ..
# ls
bindocsincludeliblibexecmysql-testsharesql-benchvar
# cd var
# ls
mysqltest
  默认情况下var目录下存放的是缺省的数据库
如果MYSQL无法初始化的话,首先得查看系统中是否存在mysql用户,如果没有得创建
第二种情况就是mysql的rpm包惹的祸,我们可以将系统中原先安装的mysql的rpm包强制卸载
rpm -e mysql --nodeps
  接下来我们得修改相关目录的权限
# pwd
/usr/local
# ll -d mysql/
drwxr-xr-x 11 root root 4096 01-19 13:35 mysql/
mysql目录的所属主和所属组都是root,进行如下修改:
# chown .mysql mysql/ -R
# ll -d mysql/
drwxr-xr-x 11 root mysql 4096 01-19 13:35 mysql/
  还有var目录
# pwd
/usr/local/mysql
# ll -d var
drwx------ 4 root mysql 4096 01-19 13:35 var
# chown mysql var -R
# ll -d var
drwx------ 4 mysql mysql 4096 01-19 13:35 var
  接下来我们就来启动mysql
# ls
bindocsincludeliblibexecmysql-testsharesql-benchvar
# cd bin
# ./mysqld_safe --user=mysql &
6374
# 100119 13:50:20 mysqld_safe Logging to '/usr/local/mysql/var/stu15.uplooking.com.err'.
100119 13:50:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
  #
查看var目录下的变化:
# pwd
/usr/local/mysql/var
# ls
ibdata1      ib_logfile1stu15.uplooking.com.errtest
ib_logfile0mysql      stu15.uplooking.com.pid
查看一下进程:
# ps -e|grep mysql
6374 pts/1    00:00:00 mysqld_safe
6425 pts/1    00:00:00 mysqld
说明mysql已经启动,现在我们得测试进入MYSQL数据库
# pwd
/usr/local/mysql/bin
# ./mysql
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
修改MYSQL数据库密码:
# ./mysqladmin -u root password 123
# ./mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# ./mysql -u root -p123
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
或者:
# ./mysql -u root -p
Enter password:(在这里输入密码)
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 5
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
MYSQL默认情况下提供了一个启动脚本在如下目录下:
# pwd
/usr/local/mysql/share/mysql
# ll mysql.server
-rwxr-xr-x 1 root mysql 12087 01-19 12:08 mysql.server
让其成为独立服务,并使其可以用service ----- stop/start/restart来操作
# pwd
/usr/local/mysql/share/mysql
# cp mysql.server /etc/init.d/
# chkconfig --add mysql.server
# service mysql.server restart
# service mysql.server restart
Shutting down MySQL.                                       [确定]
Starting MySQL.                                          [确定]
# chkconfig --list mysql.server
mysql.server    0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
这个时候每次进入数据库都得进入mysql的安装目录的bin目录下执行命令:./mysql,为啥不进行从简呢?
# which mysql
/usr/bin/which: no mysql in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin)
  vim /root/.bash_profile
10 PATH=$MYSQL_HOME:$PATH:$HOME/bin
11 MYSQL_HOME=/usr/local/mysql/bin
# source .bash_profile
# which mysql
/usr/local/mysql/bin/mysql
# echo $PATH
/usr/local/mysql/bin::/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/root/bin:/root/bin
# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -u root -p123
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
  如何为数据库修改密码呢?
# mysqladmin -u root password 123456 -p123
# mysql -u root -p123456
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
  介绍几种创建数据库的方法:
# ls
discuz   ib_logfile0mysqlstu15.uplooking.com.errtest
ibdata1ib_logfile1phpbbstu15.uplooking.com.pid
# mkdir db1
# chown mysql.mysql db1 -R
# chmod 700 db1 -R
# mysql -u root -p123456
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 5
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database               |
+--------------------+
| information_schema |
| db1                           |
| discuz                         |
| mysql                        |
| phpbb                         |
| test                           |
+--------------------+
6 rows in set (0.08 sec)
mysql>
或者用:
# mysqladmin create db1 -u root -p123456
所以说创建数据库有三种方法。对应的删除数据库也同样有三种方法。
  
安装PHP
# tar jxvf php-5.2.9.tar.bz2 -C /usr/local/src
# pwd
/usr/local/src/php-5.2.9
# ls
acconfig.h      main                              README.TESTING
acconfig.h.in   makedist                        README.TESTING2
acinclude.m4      Makefile.frag                     README.UNIX-BUILD-SYSTEM
aclocal.m4      Makefile.gcov                     README.UPDATE_5_2
build             Makefile.global                   README.WIN32-BUILD-SYSTEM
buildconf         makerpm                           README.Zeus
buildconf.bat   missing                           regex
**************************部分省略************************************
编译前的准备:
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php
编译:
make
安装:
make install
  # pwd
/usr/local/php
# ls
binetcincludelibman
# pwd
/usr/local/src/php-5.2.9
# cp php.ini-dist /usr/local/php/php.ini
将该目录下的php.ini-dist拷贝到php的安装目录下,并改名字为php.ini
如果一切顺利的话,apache2/modules 下应该有个libphp5.so 的文件
# ll /usr/local/apache2/modules/libphp5.so
-rwxr-xr-x 1 root root 12814525 01-19 14:20 /usr/local/apache2/modules/libphp5.so
接下来我们来修改apache的主配置文件,为apache添加php支持
# vim /usr/local/apache2/conf/httpd.conf
98 LoadModule rewrite_module modules/mod_rewrite.so
99 LoadModule php5_module      modules/libphp5.so
211 <IfModule dir_module>
212   DirectoryIndex index.html index.php
213 </IfModule>
353   AddType application/x-compress .Z
354   AddType application/x-gzip .gz .tgz
355   AddType application/x-httpd-php .php
重新启动apache服务
apachectl stop
apachectl start
制作测试网页
# ll /usr/local/apache2/htdocs/
总计 8
-rw-r--r-- 1 root root 44 2004-11-21 index.html
-rw-r--r-- 1 root root 22 01-19 14:31 index.php
浏览器访问,即可!
文件index.php的内容:
1 <?
2   phpinfo()
3 ?>
测试php和MYSQL的连接情况,查看1.php的内容:
1 <?php
2    mysql_connect("localhost","root","123") or die("NOT SCUSSFUL");
3    echo "OK!";
4 ?>
访问测试:http://192.168.0.158/1.php
  
安装phpBB
# pwd
/root/lamp
# ll -d phpBB/
drwxr-xr-x 12 root root 4096 01-19 09:33 phpBB/
# cp -r phpBB/ /usr/local/apache2/htdocs/
# ls
admin      db             includes      modcp.php    viewforum.php
cache      docs         index.php       posting.phpviewonline.php
cnphpbb.txtextension.incinstall         privmsg.phpviewtopic.php
common.php   faq.php      language      profile.php
config.php   groupcp.php    login.php       search.php
contrib      images         memberlist.phptemplates
# ll config.php
-rwxr-xr-x 1 root root 0 01-19 15:06 config.php
# chmod 666 config.php
# ll config.php
-rw-rw-rw- 1 root root 0 01-19 15:06 config.php
创建数据库:
# mysql -u root -p123456
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 15
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> create database phpbb;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| discuz             |
| mysql            |
| phpbb            |
| test               |
+--------------------+
5 rows in set (0.00 sec)
打开浏览器输入:http://192.168.0.158/phpBB进行安装
删除install/和contrib/目录
# ls
admin      db             includes      modcp.php    viewforum.php
cache      docs         index.php       posting.phpviewonline.php
cnphpbb.txtextension.incinstall         privmsg.phpviewtopic.php
common.php   faq.php      language      profile.php
config.php   groupcp.php    login.php       search.php
contrib      images         memberlist.phptemplates
# pwd
/usr/local/apache2/htdocs/phpBB
# rm -rf install/
# rm -rf contrib/
  呵呵!该论坛就算是搭建成功了!用户可以进行注册!

  但是该论坛可观性不怎么好!下面我们来搭建一个非常好看的论坛:discuz
相关软件包:
# pwd
/root/lamp
# ll phpMyAdmin-3.1.4-all-languages.tar.bz2
-rwxr-xr-x 1 root root 2659480 01-19 09:33 phpMyAdmin-3.1.4-all-languages.tar.bz2
解压:
# unzip Discuz/!_6.0.0_SC_GBK.pcpop.zip
# ls
Discuz!_6.0.0_SC_GBK.pcpop.zipuploadusersguideusers_guide.htmutilities
解压后把upload目录复制到/usr/local/apache2/htdocs目录下,改名为bbs
# pwd
/usr/local/apache2/htdocs
# ls
1.phpbbsindex.htmlindex.phpphpBB
修改权限:
# chown daemon bbs -R
# ll -d bbs
drwxr-xr-x 16 daemon daemon 4096 01-19 15:43 bbs
其中daemon是apache服务的用户
  建立数据库:
# mysql -u root -p123456
Welcome to the MySQL monitor.Commands end with ; or /g.
Your MySQL connection id is 25
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> create database discuz;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| discuz             |
| mysql            |
| phpbb            |
| test               |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all on discuz.* to discuz@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
用root用户不安全,这里我们用discuz用户。
  测试:访问:http://192.168.0.158/bbs/install.php即可进行安装了!!
到此discuz论坛就搭建成功了!
  
  用浏览器来管理MYSQL
相关软件:
# ll phpMyAdmin-3.1.4-all-languages.tar.bz2
-rwxr-xr-x 1 root root 2659480 01-19 09:33 phpMyAdmin-3.1.4-all-languages.tar.bz2
将其解压到apache网页存放目录中
# tar jxvf phpMyAdmin-3.1.4-all-languages.tar.bz2 -C /usr/local/apache2/htdocs/
# pwd
/usr/local/apache2/htdocs
# ls
1.phpbbsindex.htmlindex.phpphpBBphpMyAdmin-3.1.4-all-languages
# mv phpMyAdmin-3.1.4-all-languages/ pma
# ls
1.phpbbsindex.htmlindex.phpphpBBpma
修改配置文件:
# pwd
/usr/local/apache2/htdocs/pma
# ll config.sample.inc.php
-rw-r--r-- 1 root root 2095 2009-04-25 config.sample.inc.php
# mv config.sample.inc.php config.inc.php
好了!配置已经完成了!
测试:打开浏览器输入:http://192.168.0.158/pma即可,输入MYSQL的用户名和密码,就可以登录MYSQL服务器
  
             版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 用源代码搭建LAMP环境