CentOS7 搭建 LAMP環境

更改軟件源

如果你CentOS安裝時,選擇的跟我一樣是最新安裝。

一、配置網絡連接

    $> ip addr

查看自己的網卡

`$> cat /etc/sysconfig/network-scripts/ifcfg-en* // 查看對應的網`卡信息

從上面的配置中有看到雖然BOOTPROTO=dhcp,但是ONBOOT=no,這裏用vi將ONBOOT=no改成ONBOOT=yes,然後重啓CentOS。

二、設置靜態IP

eno16777736 是我的網卡

$> vi /etc/sysconfig/network-scripts/ifcfg-eno1677BOOTPROTO=dhcp
IPADDR0=192.168.235.120
PREFIX0=24
GATEWAY0=192.168.235.2
DNS1=114.114.114.114
DNS2=114.114.115.115

安裝工具

$> yum install net-tools, make, gcc, kernel-devel, ntsysv, tree, links, wget

三、安裝虛擬機增強工具

做好上述工具安裝準備,再安裝增強工具。
純命令行模式下是不支持中文的

四、配置LAMP環境

1、 查看系統初始化環境

cat /var/log/messages | grep error (檢查有無系統級錯誤信息)
dmesg (檢查硬件設備是否有錯誤信息)
cat /proc/cpuinfo (檢查 CPU 頻率是否正常)
top (按 1 檢測 CPU 核數是否正常,內存大小是否正常)
ifconfig(檢查網卡設置是否正確)
ping www.qq.com(檢查網絡是否正常)

2、關閉不需要的服務

執行 ntsysv 命令:
ntsysv // linux下配置開機自啓動服務的命令ntsysv

在服務設置界面,設置各項服務的開關。以下僅列出需要啓動的服務,未列出的服務一律推薦關閉:

    atd
    crond
    irqbalance
    microcode_ctl
    network
    sendmail
    sshd
    syslog

關閉 SElinux ,關閉方法如下:
修改 /etc/selinux/config 文件中的 SELINUX= 爲 disabled。

3、 配置Apache環境

    yum -y install httpd
    rpm -qi httpd // 查看包詳細信息

增加剛纔分配給 Apache 的端口通過防火牆,然後重新加載防火牆。

修改配置文件

    cd /etc/httpd/conf
    cp httpd.conf httpd.conf.origin
    more httpd.conf
    如果你想更改 Apache HTTP 服務器的默認端口號(80)爲其它端口,你需要編輯配置文件 ‘/etc/httpd/conf/httpd.conf’ 並查找以下面開始的行:
    LISTEN 80
    把端口號 ‘80’ 改爲其它任何端口(例如 3221),保存並退出。

HTTP服務器已經啓動,進行一下簡單配置

    vi /etc/httpd/conf/httpd.conf #編輯文件
    ServerSignature On  #添加,在錯誤頁中顯示Apache的版本,Off爲不顯示
    Options Indexes FollowSymLinks  #修改爲:Options Includes ExecCGI FollowSymLinks(允許服務器執行CGI及SSI,禁止列出目錄)
    #AddHandler cgi-script .cgi #修改爲:AddHandler cgi-script .cgi .pl (允許擴展名爲.pl的CGI腳本運行)
    AllowOverride None  #修改爲:AllowOverride All (允許.htaccess)
    AddDefaultCharset UTF-8 #修改爲:AddDefaultCharset GB2312 (添加GB2312爲默認編碼)
    #Options Indexes FollowSymLinks   #修改爲 Options FollowSymLinks(不在瀏覽器上顯示樹狀目錄結構)
    DirectoryIndex index.html   #修改爲:DirectoryIndex index.html index.htm Default.html Default.htm index.php(設置默認首頁文件,增加index.php)
    MaxKeepAliveRequests 500  #添加MaxKeepAliveRequests 500 (增加同時連接數)

重啓服務

systemctl restart httpd.service

設置爲自動啓動

systemctl enable httpd.service  //在centos7中chkconfig httpd on 被替換成 systemctl enable httpd

定製防火牆 管理員權限

In CentOS 7.0 uses Firewall-cmd, so I will customize it to allow external access to port 80 (http) and 443 (https).

    firewall-cmd --permanent --zone=public --add-service=http 
    firewall-cmd --permanent --zone=public --add-service=https
    firewall-cmd --reload

開啓了防火牆,客戶機就能通過瀏覽器訪問了

links 127.0.0.1 // 很cool

4、 安裝準備工作 安裝最新的epel

    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
    yum -y install epel-release

5、 安裝數據庫 MariaDB

參考

yum -y install mariadb-server mariadb

systemctl start mariadb.service

設置開機啓動

systemctl enable mariadb.service

設置密碼

mysql_secure_installation

回車

配置

cp /usr/share/mysql/my-huge.cnf /etc/my.cnf  //覆蓋原配置就好了。

爲什麼使用MariaDB呢?

MariaDB is the free MySQL fork, MariaDB is compatible with MySQL and the commands from MariaDB are still nemad "mysql" or "mysqldump", so there are no differences from the Client side. All Major Linux Distributions replaced MySQL with MariaDB now, so we will use that for the tutorials as well. Some users also Report that MariDB is faster then MySQL in the latest releases.

6、 安裝php5

yum -y install php

重啓httpd服務

systemctl restart httpd.service

配置:

 vi /etc/php.ini
 date.timezone = PRC #把前面的分號去掉,改爲date.timezone = PRC
    disable_functions = passthru,exec,system……#列出PHP可以禁用的函數,如果某些程序需要用到這個函數,可以刪除,取消禁用。
    expose_php = Off #禁止顯示php版本的信息
    short_open_tag = ON #支持php短標籤
    open_basedir = .:/tmp/  #設置表示允許訪問當前目錄(即PHP腳本文件所在之目錄)和/tmp/目錄,可以防止php木馬跨站,如果改了之後安裝程序有問題(例如:織夢內容管理系統),可以註銷此行,或者直接寫上程序的目錄/data/www.osyunwei.com/:/tmp/

測試一下:

vi /var/www/html/index.php  
輸入  
<?php phpinfo(); ?>

wq保存退出。

打開網頁,如果能看到PHP配置信息頁,說明PHP服務器正常。

7、測試

vi /var/www/html/info.php
輸入
<?php
    phpinfo();
?>

客戶機訪問網站

8、 獲得php的mysql支持

yum search php
yum -y install php-mysql

安裝php模塊

yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

重啓服務

 systemctl restart httpd.service

9.、安裝phpMyAdmin

    yum install phpMyAdmin

配置phpMyAdmin

    vi /etc/httpd/conf.d/phpMyAdmin.conf

註釋

    #<Directory /usr/share/phpMyAdmin/>
    #   <IfModule mod_authz_core.c>
    #     # Apache 2.4
    #     <RequireAny>
    #       Require ip 127.0.0.1
    #       Require ip ::1
    #     </RequireAny>
    #   </IfModule>
    #   <IfModule !mod_authz_core.c>
    #     # Apache 2.2
    #     Order Deny,Allow
    #     Deny from All
    #     Allow from 127.0.0.1
    #     Allow from ::1
    #   </IfModule>
    #</Directory>

添加

    <Directory /usr/share/phpMyAdmin/>
        Options none
        AllowOverride Limit
        Require all granted
    </Directory>

10.、將phpMyAdmin的認證 從cookie 修改爲 http

vi /etc/phpMyAdmin/config.inc.php

更改

$cfg['Servers'][$i]['auth_type']     = 'http';    // Authentication method (config, http or cookie based)?

在客戶端瀏覽器中登錄phpMyAdmin時,用戶名和密碼爲數據庫的用戶名和密碼

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