rpm實現LAMP部署
LAMP概述
LAMP指的Linux(操作系統)、ApacheHTTP 服務器,MySQL(有時也指MariaDB,數據庫軟件) 和PHP(有時也是指Perl或Python) 的第一個字母,一般用來建立web應用平臺。常用來搭建動態網站或者服務器的開源軟件,本身都是各自獨立的程序,但是因為常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個強大的Web應用程序平臺。
資源類型
靜態資源:原始形式與響應給客戶端的結果一致;
動態資源:原始形式通常為程序文件(為某種編程語言開發),需要運行后將生成的結果展示給客戶端;
CGI
Common Gateway Interface,是外部應用程序(CGI程序)與Web服務器之間的接口標準,是在CGI程序和Web服務器之間傳遞信息的過程。CGI規范允許Web服務器執行外部程序,并將它們的輸出發送給Web瀏覽器,CGI將Web的一組簡單的靜態超媒體文檔變成一個完整的新的交互式媒體。
httpd+php
module:可基于模塊
prefork:libphp
worker, event:libphp-zts
FastCGI
php以fpm機制獨立地監聽在一個套接字上;
工作模式類似于httpd的prefork;
基于module安裝LAMP
本次以CentOS 7為例進行安裝,部署過程中會說明CentOS 6與CentOS 7的不同之處
安裝httpd
[root@centos ~]# yum -y install httpd #安裝httpd
[root@centos ~]# systemctl start httpd.service #CentOS 7啟動服務
[root@centos ~]# service httpd start #CentOS 6啟動服務
安裝php
[root@centos7 ~]# yum -y install php php-mysql
php配置文件:
/etc/php.ini
, /etc/php.d/*.ini
php:生成libphp5.so模塊 支持動態解析
php-mysql:php驅動mysql
安裝mysql
[root@centos ~]# yum -y install mariadb-server #CentOS 7
[root@centos ~]# systemctl start mariadb.service #CentOS 7
[root@centos ~]# yum -y install mysql-server #CentOS 6
[root@centos ~]# service mysqld start #CentOS 6
初始化數據庫
[root@centos7 ~]# mysql_secure_installation #初始化配置數據庫命令
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y #是否設置root用戶密碼
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #是否刪除匿名用戶
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n #是否關閉root用戶遠程登錄功能
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #刪除test庫
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #刷新授權
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
驗證
[root@centos ~]# systemctl restart httpd.service #重啟httpd服務并驗證
基于php-fpm搭建LAMP
前面已經講解過httpd和mysql安裝,此處不再做介紹
php-fpm安裝
[root@centos ~]# yum -y install php-fpm #CentOS 7
[root@CentOS ~]# yum -y install php-fpm mod_proxy_fcgi #CentOS 6 需要安裝mod_proxy_fcgi來提供fcgi模塊,由EPEL源提供
啟動php-fpm服務
[root@centos ~]# systemctl start php-fpm.service #CentOS 7
[root@centos ~]# service php-fpm start #CentOS 6
php-fpm配置文件
ini:配置php解釋器工作環境;
/etc/php.ini, /etc/php.d/*.ini
conf:配置fpm守護進程的工作模式;
/etc/php-fpm.conf, /etc/php-fpm.d/*.conf
配置項:
/etc/php-fpm.conf
[global]
include=/etc/php-fpm.d/*.conf # 包含/etc/php-fpm.d/*.conf的所有配置文件
pid = /run/php-fpm/php-fpm.pid # 進程的pid文件路徑
error_log = /var/log/php-fpm/error.log # 錯誤日志文件路徑
log_level = notice # 定義日志文件級別
emergency_restart_threshold = 0 # 緊急情況重啟進程的條件,一般禁用
emergency_restart_interval = 0 # 緊急情況重啟進程的時間間隔,一般為禁用
process_control_timeout = 0 # 控制進程的超時時長
daemonize = no # 是否運行為守護進程,在CentOS 7上可以可配置為no,因為所有進程都由systemd管理
/etc/php-fpm.d/*.conf
[pool-id]
listen = 127.0.0.1:9000 # 進程所監聽的端口
listen.backlog = -1 # 超出請求時的隊列長度,-1表示沒有上線
listen.allowed_clients = 127.0.0.1 # 所允許的客戶端列表
user = apache # 運行子進程的用戶
group = apache # 運行子進程的組
pm = dynamic
定義process管理機制:static, dynamic
static:服務啟動時創建出所有子進程;
dynamic:根據用戶請求量的變化來維護子進程數量;
pm.max_children = 50 # 最大子進程數
pm.start_servers = 5 # 服務開啟時所啟動的子進程數
pm.min_spare_servers = 5 # 最少空閑子進程數
pm.max_spare_servers = 35 # 最大空閑子進程數
pm.max_requests = 500 # 每個子進程所響應的最大請求數
pm.status_path = /fpm-status # php-fpm的狀態頁
ping.path = /ping # 向服務器發送ping
ping.response = pong # 如果服務器正常,則回復pong
request_terminate_timeout = 0 # 定義請求超時時長,0表示無限制
request_slowlog_timeout = 0 # 定義滿請求的時間,如果達到時長,則輸出至慢請求日志當中
slowlog = /var/log/php-fpm/www-slow.log # 慢日志文件路徑
rlimit_files = 1024 # 限制用戶請求的文件數
rlimit_core = 0 #
php_admin_value[error_log] = /var/log/php-fpm/www-error.log # 連接池的錯誤日志
php_admin_flag[log_errors] = on # 管理功能的標志位是否打開
php_value[session.save_handler] = files # php基于什么方式保存session
php_value[session.save_path] = /var/lib/php/session # session文件的存放路徑
fpm配置虛擬主機反代配置
[root@centos ~]# systemctl restart httpd.service ##重啟httpd服務
測試
測試狀態頁以及ping頁面
狀態頁參數講解
http://WEB_SERVER:PORT/pm-status
pool: www # 連接池名稱
process manager: dynamic # 進程管理器類型
start time: 26/Sep/2016:15:10:26 +0800 # 啟動時間
start since: 7437 # 運行時長
accepted conn: 6 # 連接池已經處理過的總請求數
listen queue: 0 # 隊列的長度
max listen queue: 0 # 請求隊列的最大長度
listen queue len: 128 # socket等待隊列的最大長度;
idle processes: 4 # 空閑的進程數;
active processes: 1 # 活躍的進程數量;
total processes: 5 # 總進程數;
max active processes: 1 # 連接池當中過去最大活躍進程度;
max children reached: 0 # 進程數量達到連接池上限的次數;
slow requests: 0 # 慢請求的數量;
其它格式的輸出:
/pm-status?json
/pm-status?xml
/pm-status?html
/pm-status?full
full格式的輸出:
pid: 33095
state: Idle # 當前進程的狀態,idle, running, ...
start time: 26/Sep/2016:15:10:26 +0800 # 進程啟動的日期時間
start since: 7968 # 運行時長
requests: 2 # 處理過的請求數量
request duration: 112 # 請求處理過程的時長
request method: GET # 請求方法
request URI: /pm-status?html # 請求的URL
content length: 0 # 請求內容的長度,POST方法才有意義
user: - # 用戶
script: - # php腳本;
last request cpu: 0.00 # 最近一次請求消耗CPU
last request memory: 262144 # 最近一次請求消耗的內存量
測試ping頁面
部署應用
部署phpMyadmin
[root@centos ~]# yum -y install php-mbstring php-mcrypt php-gd #安裝php相關包
[root@centos ~]# unzip phpMyAdmin-4.0.5-all-languages.zip #解壓phpMyadmin包
[root@centos ~]# mv phpMyAdmin-4.0.5-all-languages/* /var/www/html/ #將文件移動至站點根目錄
測試
部署wordpress
配置數據庫
[root@centos7 ~]# mysql -uroot -pmageedu #以root身份登錄數據庫
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES; #顯示所有數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> CREATE DATABASE wordpress; #創建wordpress數據庫
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%' IDENTIFIED BY 'mageedu'; #wordpress用戶對wordpress數據庫所有權
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> FLUSH PRIVILEGES; #刷新授權
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> quit #退出
Bye
[root@centos7 ~]#
部署
[root@centos7 ~]# unzip wordpress-4.5.3-zh_CN.zip #解壓wordpress
[root@centos7 ~]# mv wordpress/* /var/www/html/ #mv文件至網站訪問根目錄
[root@centos7 ~]# cd /var/www/html/
[root@centos7 html]# cp wp-config-sample.php wp-config.php #生成配置文件
[root@centos7 html]# vim wp-config.php #編輯配置文件
部署Discuz論壇
配置數據庫
[root@centos7 ~]# mysql -uroot -pmageedu
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE bbs; #創建bbs數據庫
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON bbs.* TO 'bbs'@'%' IDENTIFIED BY 'mageedu'; #授權bbs用戶對bbs數據庫所有權
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> FLUSH PRIVILEGES; #刷新授權
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> quit
Bye
[root@centos7 ~]#
部署
[root@centos7 ~]# unzip Discuz_X3.2_SC_UTF8.zip #解壓
[root@centos7 ~]# mv upload/* /var/www/html/ #移至網站根目錄
[root@centos7 ~]# cd /var/www/html/
[root@centos7 html]# chmod -R a+w data/ config/ uc_client/ uc_server/ #對于指定目錄賦予寫入權限
原創文章,作者:zhai796898,如若轉載,請注明出處:http://www.www58058.com/56969