Ubuntu 16.04下的LAMP环境搭建

基本设置

开启root账号SSH登录

腾讯云的Ubuntu默认账号是ubuntu,且无法以root身份远程登录,首先需要手动开启远程登录root账号并设置root密码。

1
vim /etc/ssh.sshd_config

将其中的PermitRootLogin prohibit-password改为PermitRootLogin yes

设置root密码

1
sudo passwd

设置DNS

有些服务器提供商并未设置DNS地址,这时需要先手动进行设置,否则接下来的步骤都将无法解析地址。

  1. 首先打开/etc/resolv.conf/etc/resolvconf/resolv.conf.d/tail,前者在重启后会还原。
1
sudo vi /etc/resolv.conf

或者

1
sudo vi /etc/resolvconf/resolv.conf.d/tail
  1. 在文件中加入nameserver 114.114.114.114这么一条配置,保存退出。

  2. 重启网络。

1
sudo /etc/init.d/networking restart

更新源

1
sudo apt-get update

源保存的文件为:/etc/apt/sources.list

安装常用软件

SSH

1
sudo apt-get install openssh-server

查看状态:service ssh status/start/stop/restart 或:/etc/init.d/ssh status/start/stop/retsrt

Vim

1
sudo apt-get install vim

Tree

1
sudo apt-get install tree

Git

1
sudo apt-get install git

搭建LAMP

Apache

安装

1
sudo apt-get install apache2

测试

  • 查看状态: service apache2 status/start/stop/restart Web
  • 目录:/var/www
  • 安装目录: /etc/apache2/
  • 全局配置: /etc/apache2/apache2.conf
  • 监听端口: /etc/apache2/ports.conf
  • 虚拟主机: /etc/apache2/sites-enabled/000-default.conf

MySQL

安装

1
sudo apt-get install mysql-server mysql-client

测试

  • 测试:mysql -u root -p
  • 查看状态:service mysql status/start/stop/retart
  • 查看监听端口的情况:netstat -tunpl或 netstat -tap

PHP

安装

1
sudo apt-get install php7.0

测试

1
php7.0 -v

安装其他模块

1
2
sudo apt-get install libapache2-mod-php7.0
sudo apt-get install php7.0-mysql

重启服务

1
service apache2 restart service mysql restart

测试Apache能否解析

1
PHP vim /var/www/html/phpinfo.php

文件中写:<?php echo phpinfo();?>
浏览器访问:http://localhost/phpinfo.php,出现 PHP Version 网页

修改权限

1
sudo chmod 777 /var/www

phpMyAdmin

安装

1
sudo apt-get install phpmyadmin

选择apache2,点击确定。下一步选择是要配置数据库,并输入密码。

创建phpMyAdmin快捷方式

1
sudo ln -s /usr/share/phpmyadmin /var/www/html

启用Apache mod_rewrite模块

1
sudo a2enmod rewrite 

重启服务

1
service php7.0-fpm restart service apache2 restart 

测试

浏览器访问:http://localhost/phpmyadmin

配置Apache

1
vim /etc/apache2/apache2.conf 

添加AddType application/x-httpd-php .php .htm .html AddDefaultCharset UTF-8并重启Apache服务

设置

Apache限制IP访问某个特定的文件或文件夹

详细教程可以参考:「Files Directive」
以限制WordPress后台登录画面的文件wp-login.php为例:
若想只允许 IP 地址192.168.2.1访问,可按照如下设定,写入 Apache 的配置文件apache2.conf或者网站根目录下的.htaccess

1
2
3
4
5
<Files "wp-login.php">
Order Deny,Allow
Deny from all
Allow from 192.168.2.1
</Files>

只允许某 IP 地址段「192.168.2.*」访问,则为:

1
2
3
4
5
<Files "wp-login.php">
Order Deny,Allow
Deny from all
Allow from 192.168.2
</Files>

允许多个 IP 地址「192.168.2.1」「192.168.2.2」访问,则为:

1
2
3
4
5
<Files "wp-login.php">
Order Deny,Allow
Deny from all
Allow from 192.168.2.1 192.168.2.2
</Files>

Apache开启Rewrite功能

Ubuntu默认未开启Rewrite支持需手动开启,在此只是简单介绍,详细教程还是参考「Files Directive」
在配置文件apache2.conf中加入:

1
LoadModule rewrite_module modules/mod_rewrite.so 

同时打开指定目录的AllowOveride开关(以网站根目录www为例):

1
Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all 

测试Rewrite是否打开可在.htaccess中加入:

1
2
3
#BEGIN
RewriteEngine on RewriteBase / RewriteRule .*$ http://欲跳转的页面
#END

若能成功跳转,则说明Rewrite开启成功。

--- 本文结束  The End ---