學習一個服務的過程:php
一、此服務的概述:名字、功能、特色、端口號html
二、安裝python
三、配置文件的位置mysql
四、服務啓動關閉腳本,查看端口linux
五、此服務的使用方法c++
六、修改配置文件,實戰舉例正則表達式
七、排錯(從上到下,從內到外)sql
LAMP=linux+apache+mysql+php數據庫
安裝方法:apache
rpm:安裝,部署,管理比較方便,運行穩定,功能和性能較差;
源碼:安裝,部署,管理比較麻煩,運行相對不穩定,版本教新,功能和性能較高;
Apache官網:http://httpd.apache.org/
搭建思路:
一、服務器安裝LINUX系統環境,配置網絡環境,配置網絡源;
二、準備軟件包並上傳
三、安裝前先安裝make,gcc,openssl等便宜工具和開發包
四、編譯安裝。
須要源碼編譯安裝的軟件包
httpd-2.4.25.tar.gz #Apache主程序包
apr-1.5.2.tar.gz #Apache依賴包 可移植運行庫
apr-util-1.5.4.tar.gz #Apache依賴包 可移植運行庫
pcre-8.38.zip #Apache依賴包 是一個Perl庫,包括 perl 兼容的正則表達式庫
下載源碼安裝包
http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.25.tar.gz
http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz
http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz
https://sourceforge.net/projects/pcre/files/pcre/8.38/pcre-8.38.tar.gz
安裝以前請先安裝make、gcc、openssl等編譯工具和開發包
[root@xuegod71]# yum install -y cmake gcc gcc-c++ bison ncurses-devel
編譯安裝依賴包apr-1.5.2.tar.gz
[root@xuegod71 LAMP]# tar xvf apr-1.5.2.tar.gz;cd apr-1.5.2&& ./configure -prefix=/usr/local/apr && make -j 4 && make install
編譯安裝依賴包apr-util-1.5.4.tar.gz
[root@xuegod71 LAMP]# tar xvf apr-util-1.5.4 && cd apr-util-1.5.4 && ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/bin/apr-1-config && make -j && make install
安裝apr-util以前必須安裝apr。
如在已經安裝apr的前提下安裝apr-util,進入apr-util的解壓目錄而後執行如下命令:
./configure --prefix=你要安裝的目錄 --with-apr=你的apr安裝的目錄
編譯安裝依賴包pcre-8.38. tar.gz
[root@xuegod71 LAMP]# tar xvf pcre-8.38.tar.gz && cd pcre-8.38 && ./configure --prefix=/usr/local/pcre && make -j 4 && make install
便宜安裝apache
[root@xuegod71 LAMP]#tar zxvf httpd-2.4.25.tar.gz -C /usr/local/src/ && cd /usr/local/src/httpd-2.4.25/ && ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/ &&
make -j 4 && make install
注:
--enable-so #支持動態加載模塊
--enable-rewrite #支持網站地址重寫
--enable-ssl #支持ssl加密
--with-apr=/usr/local/apr #關聯apr
--with-apr-util=/usr/local/apr-util #關聯apr-util
--with-pcre=/usr/local/pcre #關聯pcre
[root@xuegod71 LAMP]# ls /usr/local/apache/conf/httpd.conf
/usr/local/apache/conf/httpd.conf ----配置文件
[root@xuegod71 LAMP]# ls /usr/local/apache/htdocs/
index.html ---網站根目錄
生成啓動腳本:
[root@xuegod71 ~]#cp /usr/local/apache/bin/apachectl /etc/init.d/
[[root@xuegod71 ~]#chmod +x /etc/init.d/apachectl
[root@xuegod71 ~]#vi !$
#!/bin/sh
#chkconfig:345 64 36
345是啓動級別;64是啓動優先級;36是關閉的優先級;
兩行是必不可少的,後面是描述信息
[root@xuegod71 ~]#chkconfig --add apachectl
[root@xuegod71 ~]#chkconfig apachectl on
[root@xuegod71 ~]#chkconfig --list apachectl
apachectl 0:off 1:off 2:on 3:on 4:on 5:on 6:off
啓動服務
[root@xuegod71 ~]#service apachectl start
測試訪問
源碼編譯mysql
下載地址:
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
MySQL從5.7版本以後,boost是必須的,建議把系統自帶的boost庫卸載,源碼編譯安裝高版本
Boost庫是爲C++語言標準庫提供擴展的一些C++程序庫的總稱
[root@xuegod71 ~]# yum -y remove boost-*
卸載系統自帶的mysql
[root@xuegod71 ~]# yum -y remove mysql
[root@xuegod71 ~]# yum -y remove boost-*
卸載系統自帶的mysql
[root@xuegod71 ~]#yum -y remove mysql
安裝必要的資源包
建議使用網絡yum源,RHEL6.5光盤中自帶的軟件包版本不夠,mysql-boost-5.7.18.tar.gz的編譯對軟件包的版本要求比較高,其中cmake的版本要不低於2.8
安裝依賴包
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
添加用戶和組
[root@xuegod71 ~]# groupadd mysql
[root@xuegod71 ~]# useradd -M -s /sbin/nologin -r -g mysql mysql
建立安裝目錄和數據存放目錄
[root@xuegod71 ~]# mkdir -p /server/mysql/
生產環境添加一塊新的硬盤,建立分區sdb1並分配全部的空間
[root@xuegod71 ~]# mkdir /server
[root@xuegod71 ~]# mount /dev/sdb1 /server/
開機自動掛載
[root@xuegod71 ~]# echo "UUID=a582d669-4b00-44b5-a820-4980574f1ac7 /server ext4 defaults 0 0" >> /etc/fstab
上傳源碼包 並解壓源碼包
[root@xuegod71 /]# tar zxf mysql-boost-5.7.18.tar.gz
將boost移到/server/mysql
[root@xuegod71 /]# mv boost/ /server/mysql/
[root@xuegod71 /]# cd mysql-5.7.18
[root@xuegod71 mysql-5.7.18]# cmake -DCMAKE_INSTALL_PREFIX=/server/mysql -DMYSQL_UNIX_ADDR=/server/mysql/mysql.sock -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/server/mysql/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=l -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST= -DWITH_BOOST=/server/mysql/boost
這些編譯參數的幫助尋找方法:
http://www.mysql.com→→Documentation→→選擇對應的版本(5.7)Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options
最終的URL https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
DCMAKE_INSTALL_PREFIX:指定MySQL程序的安裝目錄,默認/usr/local/mysql
DEFAULT_CHARSET:指定服務器默認字符集,默認latin1
DEFAULT_COLLATION:指定服務器默認的校對規則,默認latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否容許本地執行LOAD DATA INFILE,默認OFF
WITH_COMMENT:指定編譯備註信息
WITH_xxx_STORAGE_ENGINE:指定靜態編譯到mysql的存儲引擎,MyISAM,MERGE,MEMBER以及CSV四種引擎默認即被編譯至服務器,不須要特別指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不編譯的存儲引擎
SYSCONFDIR:初始化參數文件目錄
MYSQL_DATADIR:數據文件目錄
MYSQL_TCP_PORT:服務端口號,默認3306
MYSQL_UNIX_ADDR:socket文件路徑,默認/tmp/mysql.sock
編譯
mysql-5.7.18.tar.gz編譯時會佔用大量的系統資源,建議使用多個核心同時進行編譯,不然可能會編譯失敗
[root@xuegod71 mysql-5.7.18]# make -j 4
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_thd_manager.cc
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/protocol_callback.cc.
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/signal_handler.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/sql_audit.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/sql_client.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/srv_session.cc.o
[ 96%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_info_serv
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_service.c
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_daemon.cc.o
Linking CXX static library libsql.a
[100%] Built target sql
Scanning dependencies of target mysqld
Scanning dependencies of target pfs_connect_attr-t
[100%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
Linking CXX executable mysqld
[100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_c
[100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_c
[100%] Building C object storage/perfschema/unittest/CMakeFiles/pfs_con
Linking CXX executable pfs_connect_attr-t
[100%] Built target pfs_connect_attr-t
[100%] Built target mysqld
Scanning dependencies of target udf_example
[100%] Building CXX object sql/CMakeFiles/udf_example.dir/udf_example.c
Linking CXX shared module udf_example.so
[100%] Built target udf_example
make -j 4 #注:-j 用來指定CPU核心數,可加快編譯速度。 加 -j 4我用4核心來編譯的,能夠提升編譯速度;固然你要肯定CPU多少核心
查看服務器CPU核心數
[root@xuegod71 mysql-5.7.18]# grep processor /proc/cpuinfo | wc -l
4
[root@xuegod71 mysql-5.7.18]#make install
-- Installing: /server/mysql/mysql-test/./README
-- Installing: /server/mysql/mysql-test/./mysql-test-run.pl
-- Up-to-date: /server/mysql/mysql-test/mtr
-- Up-to-date: /server/mysql/mysql-test/mysql-test-run
-- Installing: /server/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /server/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /server/mysql/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /server/mysql/support-files/mysqld_multi.server
-- Installing: /server/mysql/support-files/mysql-log-rotate
-- Installing: /server/mysql/support-files/magic
-- Installing: /server/mysql/share/aclocal/mysql.m4
-- Installing: /server/mysql/support-files/mysql.server
[root@xuegod71 mysql-5.7.18]#
修改目錄權限
[root@xuegod71 mysql-5.7.18]# chown -R mysql:mysql /server/mysql/
[root@xuegod71 mysql-5.7.18]# ll /server/mysql/
總用量 60
drwxr-xr-x 2 mysql mysql 4096 4月 23 12:38 bin
drwxr-xr-x 3 mysql mysql 4096 3月 18 15:57 boost
-rw-r--r-- 1 mysql mysql 17987 3月 18 15:45 COPYING
drwxr-xr-x 2 mysql mysql 4096 4月 23 12:37 docs
drwxr-xr-x 3 mysql mysql 4096 4月 23 12:37 include
drwxr-xr-x 4 mysql mysql 4096 4月 23 12:38 lib
drwxr-xr-x 4 mysql mysql 4096 4月 23 12:37 man
drwxr-xr-x 10 mysql mysql 4096 4月 23 12:39 mysql-test
-rw-r--r-- 1 mysql mysql 2478 3月 18 15:45 README
drwxr-xr-x 28 mysql mysql 4096 4月 23 12:39 share
drwxr-xr-x 2 mysql mysql 4096 4月 23 12:39 support-files
生成配置文件
[root@xuegod71 mysql-5.7.18]# mv /etc/my.cnf{,.bak}
[root@xuegod71 etc]# ll my.cnf.bak
-rw-r--r--. 1 root root 251 6月 22 2015 my.cnf.bak
[root@xuegod71 etc]# vi /etc/my.cnf
[mysql]
sql_mode=NO_ENGINE_SUBSTITUTION.STRICT_TRANS_TABLES
#default
user = mysql
basedir = /server/mysql
datadir = /server/mysql/data
port = 3306
pid-file = /server/mysql/data/mysql.pid
socket = /server/mysql/mysql.sock
character-set-server = utf8
[client]
socket = /server/mysql/mysql.sock
生成服務啓動腳本
[root@xuegod71 etc]# cp /server/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@xuegod71 etc]# chmod +x /etc/init.d/mysqld
[root@xuegod71 etc]# chkconfig --add mysqld
[root@xuegod71 etc]# chkconfig mysqld on
[root@xuegod71 etc]# chkconfig --list mysqld
mysqld 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
[root@xuegod71 etc]#
初始化數據庫
[root@xuegod71 etc]# /server/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/server/mysql --datadir=/server/mysql/data
2017-04-23T04:58:23.534832Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-23T04:58:23.969872Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-04-23T04:58:24.065194Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-04-23T04:58:24.221551Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7af6e4bd-27e1-11e7-82a4-000c297e35d2.
2017-04-23T04:58:24.224774Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-04-23T04:58:24.247060Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@xuegod71 etc]#
啓動服務
[root@xuegod71 ~]# service mysqld start
Starting MySQL SUCCESS!
添加path路徑: vim /etc/profile 添加下面2行 在文件的結尾
export MYSQL_HOME=/server/mysql
export PATH=$PATH:$MYSQL_HOME/bin
使修改生效
或
source /etc/profile
[root@xuegod71 ~]# ln -s /server/mysql/bin/* /usr/local/bin/
源碼編譯安裝php7.0.5
官網地址:http://php.net/
源碼包下載地址
http://219.239.26.3/files/4255000007F687B2/cn2.php.net/distributions/php-7.0.5.tar.gz
安裝epel擴展yum源
[root@xuegod71 ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.BHPeyO: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
package epel-release-6-8.noarch is already installed
[root@xuegod71 ~]# yum clean all
[root@xuegod71 ~]# yum list
注:RHEL光盤和CentOS源缺乏不少軟件包,建議使用擴展源進行安裝依賴包
若是擴展源不能用,用本地yum源
安裝所需依賴包
[root@xuegod71 ~]#
yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel
Verifying : 2:libpng-1.2.49-1.el6_2.x86_64 26/26
已安裝:
gd.x86_64 0:2.0.35-11.el6 libmcrypt.x86_64 0:2.5.8-9.el6
libmcrypt-devel.x86_64 0:2.5.8-9.el6 php-mcrypt.x86_64 0:5.3.3-5.el6
做爲依賴被安裝:
libXpm.x86_64 0:3.5.10-2.el6 php-common.x86_64 0:5.3.3-49.el6
更新完畢:
curl.x86_64 0:7.19.7-53.el6_9 freetype.x86_64 0:2.3.11-17.el6
libcurl-devel.x86_64 0:7.19.7-53.el6_9 libpng.x86_64 2:1.2.49-2.el6_7
libpng-devel.x86_64 2:1.2.49-2.el6_7 libxml2.x86_64 0:2.7.6-21.el6_8.1
libxml2-devel.x86_64 0:2.7.6-21.el6_8.1
做爲依賴被升級:
freetype-devel.x86_64 0:2.3.11-17.el6 libcurl.x86_64 0:7.19.7-53.el6_9
libxml2-python.x86_64 0:2.7.6-21.el6_8.1
完畢!
[root@xuegod71 ~]# tar xf php-7.0.18.tar.gz
[root@xuegod71 ~]# ls
anaconda-ks.cfg install.log.syslog mysql-boost-5.7.18.tar.gz.4
apr-1.5.2 mysql-5.7.18 pcre-8.38
apr-1.5.2.tar.gz MySQL5.7自動安裝腳本.sh pcre-8.38.tar.gz
apr-util-1.5.4 mysql-boost-5.7.18.tar.gz php-7.0.18
apr-util-1.5.4.tar.gz mysql-boost-5.7.18.tar.gz.1 php-7.0.18.tar.gz
httpd-2.4.25.tar.gz mysql-boost-5.7.18.tar.gz.2
install.log mysql-boost-5.7.18.tar.gz.3
[root@xuegod71 ~]# cd php-7.0.18
[root@xuegod71 php-7.0.18]# ./configure --prefix=/server/php7/ \
> --with-apxs2=/usr/local/apache/bin/apxs \
> --enable-mbstring \
> --with-curl \
> --with-gd \
> --enable-fpm \
> --enable-mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --with-config-file-path=/server/php7/etc/ \
> --with-mysqli=mysqlnd \
> --with-mysql-sock=/server/mysql/mysql.sock
config.status: creating php7.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/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
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@xuegod71 php-7.0.18]#
注:
--with-apxs2 #將php編譯爲Apache的一個模塊進行使用
--enable-mbstring #多字節字符串的支持
--with-curl #支持cURL
--with-gd #支持gd庫
--enable-fpm #支持構建fpm
--enable-mysqlnd #啓用mysqlnd
--with-pdo-mysql #支持pdo:MySQL支持
--with-config-file-path #設置配置文件路徑
--with-mysqli #支持MySQLi
--with-mysql-sock #關聯mysql的socket文件
[root@xuegod71 php-7.0.18]# make -j 2 && make install
Installing PEAR environment: /server/php7/lib/php/
[PEAR] Archive_Tar - installed: 1.4.2
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.2
[PEAR] PEAR - installed: 1.10.3
Wrote PEAR system config file at: /server/php7/etc/pear.conf
You may want to add: /server/php7/lib/php to your php.ini include_path
/root/php-7.0.18/build/shtool install -c ext/phar/phar.phar /server/php7/bin
ln -s -f phar.phar /server/php7/bin/phar
Installing PDO headers: /server/php7/include/php/ext/pdo/
生成配置文件
[root@xuegod71 php-7.0.18]# cp php.ini-production /server/php7/etc/php.ini
添加Apache支持
[root@xuegod71 htdocs]# vi /usr/local/apache/conf/httpd.conf
Addtype application/x-httpd-php .php .phtml
建立測試頁面
[root@xuegod71 htdocs]# vi a.php
<?php
phpinfo();
?>