Ubuntu Server 11.10安裝配置lamp(Apache+MySQL+PHP)_Linux

來源:互聯網
上載者:User

準備篇:

1、配置防火牆,開啟80連接埠、3306連接埠

說明:Ubuntu預設安裝是沒有開啟任何防火牆的,為了伺服器的安全,建議大家安裝啟用防火牆設定,這裡推薦使用iptables防火牆。
whereis iptables #查看系統是否安裝防火牆
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已經安裝iptables
apt-get install iptables #如果預設沒有安裝,請運行此命令安裝防火牆
iptables -L #查看防火牆配置資訊,顯示如下:

#####################################################
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#####################################################
nano /etc/iptables.default.rules #添加以下內容
##################################################################################################
*filter
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and MySQLconnections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 3306 -j ACCEPT
# Allows SSH connections for script kiddies
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
##################################################################################################
ctrl+o #儲存
ctrl+x #退出


備忘:80是指web伺服器連接埠、3306是指MySQL資料庫連結連接埠、22是指SSH遠端管理連接埠
iptables-restore < /etc/iptables.default.rules #使防火牆規則生效
nano /etc/network/if-pre-up.d/iptables #建立檔案,添加以下內容,使防火牆開機啟動
##########################################################
#!/bin/bash
/sbin/iptables-restore </etc/iptables.default.rules
##########################################################
chmod +x /etc/network/if-pre-up.d/iptables #添加執行許可權

安裝篇

一、安裝apache

apt-get install apache2 #安裝apache,根據提示輸入y安裝
service apache2 start #啟動apache
service apache2 restart #重啟
apt-get install chkconfig #安裝chkconfig
chkconfig apache2 on #開機啟動apache

二、安裝MySQL

apt-get install mysql-server #安裝mysql,根據提示輸入y安裝
安裝過程中,會跳出輸入root密碼的介面
輸入2次密碼,繼續自動安裝

service mysql start #啟動
chkconfig mysql on #開機啟動mysql
service mysql restart #重啟

三、安裝php

1、apt-get install php5 #安裝php5,根據提示輸入y安裝
2、安裝PHP組件,使PHP支援 MySQL
apt-get install php5-mysql php5-gd libjpeg8-dev php5-imap php5-ldap php5-odbc php*-pear php*-xml php5-xmlrpc php5-mcrypt php5-mhash libmcrypt* libmcrypt-dev php-fpdf
/etc/init.d/mysql restart #重啟mysql
/etc/init.d/apache2 restart #重啟apache2
ln -s /etc/php5/apache2/php.ini /etc/php.ini #把php設定檔連結到系統預設位置

配置篇

一、Apache配置
cp /etc/apache2/apache2.conf /etc/apache2/apache2.confbak #備份
nano /etc/apache2/apache2.conf #編輯
ServerTokens Prod #在出現錯誤頁的時候不顯示伺服器作業系統的名稱
ServerSignature Off #在在錯誤頁中不顯示apache2的版本
MaxKeepAliveRequests 1000 #修改為1000(預設為100,增加同時串連數)
ServerName localhost   #添加apache2預設服務名
ctrl+o #儲存
ctrl+x #退出
nano /etc/apache2/sites-enabled/000-default #編輯
Options MultiViews FollowSymLinks #不在瀏覽器上顯示樹狀目錄結構
ctrl+o #儲存
ctrl+x #退出
nano /etc/apache2/mods-enabled/dir.conf   #編輯,設定預設首頁順序
DirectoryIndex index.html  index.php  index.htm
ctrl+o #儲存
ctrl+x #退出
a2enmod rewrite #啟用apache2偽靜態模組mod rewrite

二、配置php

nano /etc/php5/apache2/php.ini #編輯
date.timezone = PRC #在946行 把前面的分號去掉,改為date.timezone = PRC
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在386行 列出PHP可以禁用的函數,如果某些程式需要用到這個函數,可以刪除,取消禁用。
expose_php = Off #在432行 禁止顯示php版本的資訊
magic_quotes_gpc = On #在745行 開啟magic_quotes_gpc來防止SQL注入
open_basedir = .:/tmp/ #在380行,設定表示允許訪問目前的目錄(即PHP指令檔所在之目錄)和/tmp/目錄,可以防止php木馬跨站,如果改了之後安裝程式有問題,可登出此行,或者直接寫上程式目錄路徑/var/www/www.osyunwei.com/:/tmp/
ctrl+o #儲存
ctrl+x #退出

測試篇
cd /var/www #進入預設網站目錄
nano index.php #建立測試檔案

<?phpphpinfo();?>

ctrl+o #儲存
ctrl+x #退出
chown www-data.www-data -R /var/www #添加目錄所有者
chmod 700 -R /var/www #設定目錄許可權
在用戶端瀏覽器輸入伺服器IP地址,可以看到相關的配置資訊!

備忘:

apache2預設網站目錄是:/var/www
使用權限設定:chown www-data.www-data -R /var/www
apache2虛擬機器主機設定檔:/etc/apache2/sites-enabled/000-default #虛擬機器主機設定檔
MySQL資料庫目錄是:/var/lib/mysql
使用權限設定:chown mysql.mysql -R /var/lib/mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.