搭建Linux平臺PHP、MySQL、Apache環境

Apache

簡述

本文主要講述在 vps 主機上部署 apache 服務器,首先需要購買 vps 主機,其次需要購買域名,再將域名解析到 vps 主機的 ip 地址上,以後就可以使用該域名訪問我們的服務器了。

軟件下載

進入 官網 ,點擊 Download2.4.29 (released 2017-10-23)Source: httpd-2.4.29.tar.gz [ PGP ] [ MD5 ] [ SHA1 ] [ SHA256 ] ,下載後,文件名爲:httpd-2.4.29.tar.gz

軟件安裝

安裝必要的庫

yum -y install gcc gcc-c++ openssl-devel zlib-deve pcre pcre-devel

將文件拷貝到 Linux ,解壓縮

[root@host apache]# tar xf httpd-2.4.29.tar.gz
[root@host httpd-2.4.29]# ./configure --prefix=/opt/app/apache
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
checking for APR... no
configure: error: APR not found.  Please read the documentation.

安裝 apr

官網 下載 apr ,文件名爲:apr-1.6.3.tar.gz ,解壓縮

[root@host apache]# tar xf apr-1.6.3.tar.gz
[root@host apr-1.6.3]# ./configure --prefix=/opt/app/apr
[root@host apr-1.6.3]# make
[root@host apr-1.6.3]# make install

繼續編譯

configure: error: APR-util not found. Please read the documentation.

安裝apr-util

官網 下載 apr-util ,文件名爲:apr-util-1.6.1.tar.gz ,解壓縮

[root@host apache]# tar xf apr-util-1.6.1.tar.gz
[root@host apr-util-1.6.1]# ./configure --prefix=/opt/app/apr-util --with-apr=/opt/app/apr/bin/apr-1-config
[root@host apr-util-1.6.1]# make 
xml/apr_xml.c:35:19: error: expat.h: No such file or directory
[root@host apr-util-1.6.1]# yum install expat-devel
[root@host apr-util-1.6.1]# make 
[root@host apr-util-1.6.1]# make install

繼續…

[root@host httpd-2.4.29]# ./configure --prefix=/opt/app/apache --with-apr=/opt/app/apr/bin/apr-1-config --with-apr-util=/opt/app/apr-util/bin/apu-1-config --enable-so
[root@host httpd-2.4.29]# make
[root@host httpd-2.4.29]# make install

測試

[root@host apache]# /opt/app/apache/bin/apachectl -k start
AH00557: httpd: apr_sockaddr_info_get() failed for host.localdomain
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

配置

打開 httpd.conf ,對比源文件,修改爲以下內容:

#LoadModule watchdog_module modules/mod_watchdog.so
#LoadModule xml2enc_module modules/mod_xml2enc.so

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/home/sy/XGManager/home"
<Directory "/home/sy/XGManager/home">

PHP

軟件下載

進入 官網 ,點擊 Downloads ,找到標題:php-7.2.0.tar.gz (sig) [17,849Kb] 下載的文件名:

php-7.2.0.tar.gz

編譯

安裝編譯環境

[root@host php-7.2.0]# yum groupinstall "Development tools"
[root@host php-7.2.0]# yum install libxml2-devel gd-devel libmcrypt-devel libcurl-devel openssl-devel

配置

[root@host php-7.2.0]# ./configure --prefix=/opt/app/php/php-7.2.0 --with-apxs2=/opt/app/apache/bin/apxs --disable-cli --enable-shared --with-libxml-dir --with-gd --with-openssl --enable-mbstring --with-mysqli --enable-opcache --enable-zip --with-zlib-dir --with-pdo-mysql --with-jpeg-dir --with-freetype-dir --with-curl --without-pdo-sqlite --without-sqlite3
[root@host php-7.2.0]# make
cc: Internal error: Killed (program cc1)
Please submit a full bug report.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
原因是內存不足,需要使用交換分區,增加交換分區:
[root@host php-7.2.0]# sudo dd if=/dev/zero of=/swapfile bs=64M count=16
[root@host php-7.2.0]# sudo mkswap /swapfile
[root@host php-7.2.0]# sudo swapon /swapfile
[root@host php-7.2.0]# make install
編譯完成,刪除交換分區:
[root@host php-7.2.0]# sudo swapoff /swapfile
[root@host php-7.2.0]# sudo rm /swapfile

拷貝配置文件:

[root@host php-7.2.0]# cp php.ini-development /opt/app/php/php-7.2.0/php.ini

關聯 apache

打開 httpd.conf ,對比源文件,修改爲以下內容:

#LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php7_module        modules/libphp7.so
PHPIniDir "/opt/app/php/php-7.2.0/"
AddType application/x-httpd-php .php .html .htm

配置 php

修改 php.ini 爲以下內容:

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"
extension_dir = "/opt/app/php/php-7.2.0/ext"

extension=curl
extension=gd2
extension=mbstring
extension=mysqli
extension=pdo_mysql
extension=xmlrpc

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = Asia/Shanghai

; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
;session.save_path = "/tmp"
session.save_path = "/opt/app/php/PHPSessionTmp"

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
;upload_tmp_dir =
upload_tmp_dir = "/opt/app/php/PHPFileUploadTmp"

重啓 Apache

[root@host apache]# /opt/app/apache/bin/apachectl -k restart

測試

首先 ,在目錄/home/sy/XGManager/home 放置一個文件 index.php ,輸入內容:

<?php
    phpinfo();
?>

刷新網頁,如果能夠看到以下畫面,說明 Apache 已經關聯了 PHP

這裏寫圖片描述

MySQL

軟件安裝

[root@host home]# yum install mysql
[root@host home]# yum install mysql-server
[root@host home]# yum install mysql-devel
[root@host home]# service mysqld restart

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h host.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation
Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@host home]# mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> insert into mysql.user(Host,User,Password) values('@','sy',password('********'));

重啓 Mysql

[root@host home]# service mysqld restart

登錄新用戶

[root@host home]# mysql -u sy -p

PHPMyAdmin

軟件下載

進入官網 ,點擊 download ,下載的文件名爲:phpMyAdmin-4.7.5-all-languages.zip

軟件安裝

將解壓後的文件夾重命名爲 phpMyAdmin ,放到目錄:C:\Users\SY\Desktop\GIT\Software\PHP\Apache_Workspace\PHP\HOME ,在瀏覽器輸入:鏈接 即可訪問。

測試

修改 index.php 內容:

<?php  
    $connect=mysqli_connect("127.0.0.1","root","10929131");  
    if (!$connect)
        echo "Mysql Connect Error!";  
    else 
        echo "MySQL OK!";  
    mysqli_close($connect);  
?>

看到打印:MySQL OK! ,說明連接數據庫成功!注意 PHP7 以後使用 mysqli 代替 mysql

搭建應用程序

apache 默認配置的用戶爲 daemon ,我們打開文件:/opt/app/apache/conf/httpd.conf

#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

我們修改爲:sy ,重啓 apache

 [root@host /]# /opt/app/apache/bin/apachectl -k restart
 [root@host /]# ps -ef | grep apache
  root     23485     1  0 03:03 ?        00:00:00 /opt/app/apache/bin/httpd -k start
  sy       25653 23485  0 04:02 ?        00:00:00 /opt/app/apache/bin/httpd -k start
  sy       25654 23485  0 04:02 ?        00:00:00 /opt/app/apache/bin/httpd -k start
  sy       25655 23485  0 04:02 ?        00:00:00 /opt/app/apache/bin/httpd -k start
  root     25746 20184  0 04:03 pts/0    00:00:00 grep apache

可以看到我們的 uid 變爲了 sy

當我們向網站上傳文件時,彈出:unknow:file created in the system's temporatory in Unknown on line 0 警告。

查看網站目錄的所有者:

[root@host /]# ls -al /home/sy/XGManager/
total 32
drwxr-xr-x 5 root root 4096 Dec 30 08:35 .
drwxr-xr-x 6 root root 4096 Jan  1 09:17 ..
-rwxr-xr-x 1 root root  169 Dec 30 08:35 .buildpath
drwxr-xr-x 8 root root 4096 Dec 30 08:35 .git
drwxr-xr-x 9 root   root   4096 Jan  2 03:54 home
-rwxr-xr-x 1 root root  697 Dec 30 08:35 .project
-rwxr-xr-x 1 root root   49 Dec 30 08:35 README.md
drwxr-xr-x 2 root root 4096 Dec 30 08:35 .settings

可以看到所有者爲 root ,而我們創建的 apache worker 進程的所有者爲 sy ,權限不足,因此報錯。

需要修改爲以下內容:

[root@host app]# chown -R sy:sy /home/sy/XGManager/home/

再次上傳,遇到問題:Unknown: file created in the system's temporary directory in Unknown on line 0

由於我們配置 [root@host /]# vim /opt/app/php/php-7.2.0/php.ini

Temporary directory for HTTP uploaded files (will use system default if not
specified).
http://php.net/upload-tmp-dir
upload_tmp_dir =
upload_tmp_dir = "opt/app/php/PHPFileUploadTmp"

文件上傳時,會臨時存儲到 upload_tmp_dir 指定的目錄,因此,該目錄的所有者也應該是 sy

[root@host app]# chown -R sy:sy /opt/app/php/PHPFileUploadTmp/

再次測試,上傳成功!

遇到問題:

Warning: session_start(): open(/opt/app/php/PHPSessionTmp/sess_qt1lthvdd2buesvglt0piji5l5, O_RDWR) failed: Permission denied (13) in /home/sy/XGManager/home/utils/head.php on line 39

修改用戶權限:

[root@host home]# chown sy:sy -R /opt/app/php/PHPSessionTmp/

參考教程

Linux下安裝apache http server 過程

centos下apache/ab的編譯安裝

centOS下編譯安裝php

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章