LAMP搭建示例
- 2021 年 1 月 3 日
- 筆記
lamp介紹
其實就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一組動態網站或者服務器的開源軟件,除Linux外其它各部件本身都是各自獨立的程序,但是因為經常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個強大的Web應用程序平台。
-
Apache,網頁服務器
web服務器工作流程
web服務器的資源分為兩種,靜態資源和動態資源
-
靜態資源就是指靜態內容,客戶端從服務器獲得的資源的表現形式與原文件相同。可以簡單的理解為就是直接存儲於文件系統中的資源
-
動態資源則通常是程序文件,需要在服務器執行之後,將執行的結果返回給客戶端
工作過程:當客戶端請求是靜態資源時,web服務器會直接把靜態資源返回個客戶端。
:動態資源則通過FastCGI協議與php服務器聯繫,通過CGI程序的master進程調度worker進程來執行程序以獲得客戶端請求的動態資源,並將執行的結果通過FastCGI協議返回給httpd服務器,httpd服務器收到php的執行結果後將其封裝為http響應報文響應給客戶端。在執行程序獲取動態資源時若需要獲得數據庫中的資源時,由Php服務器通過mysql協議與MySQL/MariaDB服務器交互,取之而後返回給httpd,httpd將從php服務器收到的執行結果封裝成http響應報文響應給客戶端。
cgi與fastcgi
CGI(Common Gateway Interface,通用網關接口),CGI是外部應用程序(CGI程序)與WEB服務器之間的接口標準,是在CGI程序和Web服務器之間傳遞信息的過程。CGI規範允許Web服務器執行外部程序,並將它們的輸出發送給Web瀏覽器,CGI將web的一組簡單的靜態超媒體文檔變成一個完整的新的交互式媒體。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通過啟用一個解釋器進程來處理每個請求,耗時且耗資源,而FastCGI則是通過master-worker形式來處理每個請求,即啟動一個master主進程,然後根據配置啟動幾個worker進程,當請求進來時,master會從worker進程中選擇一個去處理請求,這樣就避免了重複的生成和殺死進程帶來的頻繁cpu上下文切換而導致耗時
lamp平台搭建
環境說明:
系統平台 | IP | 需要安裝的服務 |
centos8 redhat8 |
192.168.248.131 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平台軟件安裝次序: httpd --> mysql --> php
yum源:
curl -o /etc/yum.repos.d/CentOS-Base.repo //mirrors.aliyun.com/repo/Centos-8.repo sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo #epel yum install -y //mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm sed -i 's|^#baseurl=//download.fedoraproject.org/pub|baseurl=//mirrors.aliyun.com|' /etc/yum.repos.d/epel* sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
安裝httpd
#安裝開發工具包 [root@localhost ~]# yum groups mark install 'Development Tools' #創建apache用戶和組 [root@localhost ~]# useradd -r -M -s /sbin/nologin apache #安裝依賴包 [root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make #下載和安裝apache,apr和apr-util [root@localhost ~]# wget //mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.gz [root@localhost ~]# wget //mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz [root@localhost ~]# wget //mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz [root@localhost ~]# tar -xf apr-1.7.0.tar.gz [root@localhost ~]# tar -xf httpd-2.4.46.tar.gz [root@localhost ~]# tar -xf apr-util-1.6.1.tar.gz [root@localhost ~]# ls apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.46.tar.gz apr-1.7.0 apr-util-1.6.1 httpd-2.4.46 [root@localhost ~]# cd apr-1.7.0 [root@localhost apr-1.7.0]# vim configure cfgfile="${ofile}T" trap "$RM \"$cfgfile\"; exit 1" 1 2 15 # $RM "$cfgfile" //將此行加上注釋,或者刪除此行 [root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr ....................................................配置過程 [root@localhost apr-1.7.0]# make && make install ...................................編譯安裝過程 [root@localhost apr-1.7.0]# cd /root/apr-util-1.6.1 [root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr ....................................配置過程 [root@localhost apr-util-1.6.1]# make && make install .........................................編譯安裝過程 #apache安裝 [root@localhost apr-util-1.6.1]# cd /root/httpd-2.4.46 [root@localhost httpd-2.4.46]# ./configure --prefix=/usr/local/apache \ > --sysconfdir=/etc/httpd24 \ > --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 httpd-2.4.46]# make && make install ............................編譯安裝過程 #安裝後配置 [root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh [root@localhost ~]# source /etc/profile.d/httpd.sh [root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd [root@localhost ~]# vim /etc/man_db.conf MANDATORY_MANPATH /usr/man MANDATORY_MANPATH /usr/share/man MANDATORY_MANPATH /usr/local/share/man MANDATORY_MANPATH /usr/local/apache/man //添加此行 [root@localhost ~]# vim /etc/httpd24/httpd.conf # #ServerName www.example.com:80 //取消前面的注釋 #啟動apache [root@localhost ~]# apachectl start [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:*
安裝MySQL
#安裝依賴包 [root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs #創建用戶和組 [root@localhost ~]# useradd -r -M -s /sbin/nologin mysql #下載二進制格式mysql軟件包 [root@localhost ~]# wget //downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz #解壓軟件 [root@localhost ~]# tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost ~]# ln -sv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/ /usr/local/mysql '/usr/local/mysql' -> '/usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/' [root@localhost local]# chown -R mysql.mysql mysql* #添加環境變量 [root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh [root@localhost local]# source /etc/profile.d/myslq.sh [root@localhost local]# echo $PATH /usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/apache/man
MANDATORY_MANPATH /usr/local/mysql/man #添加man文件
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig
#創建數據存放目錄 [root@localhost local]# mkdir /mydata [root@localhost local]# chown -R mysql.mysql /mydata/ #初始化數據庫 [root@localhost local]# mysqld --initialize --user=mysql --datadir=/mydata 2021-01-01T07:38:58.366227Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-01-01T07:38:58.551752Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-01-01T07:38:58.582971Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-01-01T07:38:58.593654Z 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: 68c0296b-4c04-11eb-a48e-000c2928a994. 2021-01-01T07:38:58.594268Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-01-01T07:38:59.115003Z 0 [Warning] CA certificate ca.pem is self signed. 2021-01-01T07:38:59.228479Z 1 [Note] A temporary password is generated for root@localhost: Jcselu.3md<w #修改my.cnf配置文件 [root@localhost ~]# vim /etc/my.cnf #添加以下內容 [mysqld] basedir = /usr/local/mysql datadir = /mydata socket = /tmp/mysql.sock port = 3306 pid-file = /mydata/mysql.pid user = mysql skip-name-resolve [client-server] #配置服務啟動腳本 [root@localhost ~]# cd /usr/local/mysql/support-files/ [root@localhost support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@localhost support-files]# cp mysql.server /etc/init.d/mysqld [root@localhost support-files]# vim /etc/init.d/mysqd # 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=/mydata #啟動mysql [root@localhost ~]# service mysqld start Starting MySQL. SUCCESS! [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* #使用臨時密碼登錄修改密碼 [root@localhost ~]# mysql -uroot -p'Jcselu.3md<w' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.31 Copyright (c) 2000, 2020, 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> set password = password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec)
yum安裝PHP
#安裝依賴包 [root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd [root@localhost ~]# yum -y install php-* [root@localhost ~]# php -v PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies [root@localhost ~]# vim /etc/php-fpm.d/www.conf ;listen = /run/php-fpm/www.sock #注釋此行 listen = 127.0.0.1:9000 #添加監聽端口 [root@localhost ~]# systemctl start php-fp [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22
配置apache
啟用代理模塊
[root@localhost ~]# vim /etc/httpd24/httpd.conf #LoadModule remoteip_module modules/mod_remoteip.so LoadModule proxy_module modules/mod_proxy.so //取消注釋 #LoadModule proxy_connect_module modules/mod_proxy_connect.so #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so #LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so //取消注釋 #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
創建虛擬主機目錄並生成php測試頁面
[root@localhost ~]# mkdir /usr/local/apache/htdocs/test [root@localhost ~]# vim /usr/local/apache/htdocs/test/index.php <?php phpinfo(); ?> [root@localhost ~]# chown -R apache.apache /usr/local/apache/htdocs/ [root@localhost ~]# vim /etc/httpd24/httpd.conf #在配置文件的最後加入以下內容 <VirtualHost *:80> DocumentRoot "/usr/local/apache/htdocs/test" ServerName www.testhhhh.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1 <Directory "/usr/local/apache/htdocs/test"> Options none AllowOverride none Require all granted </Directory> </VirtualHost>
[root@localhost ~]# vim /etc/httpd24/httpd.conf # If the AddEncoding directives above are commented-out, then you # probably should define those extensions to indicate media types: # AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php #添加此行 AddType application/x-httpd-php-source .phps #添加此行 ....................................................... # is requested. # <IfModule dir_module> DirectoryIndex index.php index.html #添加index.php [root@localhost ~]# apachectl restart