关于Ubuntu与Centos7下安装(Apache+PHP+Mysql+phpmyadmin)

1,Ubuntu下安装

安装LAMP

LAMP是Linux、Apache、MySql(MariaDB)、PHP(Python、Perl)等软件的合称。我们现在要在Ubuntu16.04上安装,因此只需要安装其他三个软件就可以了。

sudo apt install mysql-server-5.7 mysql-client-5.7 php7.0 apache2

对于这些软件可能还需要各自进行配置,这里就不再细述了。

配置apache

启用PHP支持

然后安装apache的php扩展:

sudo apt install libapache2-mod-php7.0

安装完成之后需要重启apache:

sudo systemctl restart apache2

然后在apache的默认目录中新建一个PHP文件:

sudo nano /var/www/html/info.php

文件内容如下:

<?php
phpinfo();
?>

然后在浏览器中查看一下是否成功:info。 
成功之后别忘了删除info.php,它包含了很多服务器的敏感信息。

sudo rm -f /var/www/html/info.php
启用SSL

然后启用SSL:

sudo a2enmod ssl
sudo a2ensite default-ssl

配置PHP

启用PHP扩展

安装所需的PHP扩展,也可以全部安装,全部安装可能会降低性能:

sudo apt -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache  php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext

然后重启apache:

sudo systemctl restart apache2
安装APCu

APCu是一个缓存扩展,可以缓存并优化PHP中间代码,强烈建议安装。

sudo apt -y install php-apcu

然后重启apache:

sudo systemctl restart apache2

Ubuntu上安装MySQL非常简单只需要几条命令就可以完成。

1. sudo apt-get install mysql-server

2. apt-get isntall mysql-client

3.  sudo apt-get install libmysqlclient-dev

安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后可以使用如下命令来检查是否安装成功:

sudo netstat -tap | grep mysql

通过上述命令检查之后,如果看到有mysql 的socket处于 listen 状态则表示安装成功。

登陆mysql数据库可以通过如下命令:

mysql -u root -p

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,上面命令输入之后会提示输入密码,此时输入密码就可以登录到mysql。


安装phpmyadmin

上面的工作全部完成之后,就可以安装phpmyadmin了。

sudo apt -y install phpmyadmin
接着,安装完成后,去服务器目录下检查,发现并没有phpmyadmin,这样的文件或者文件夹

系统在安装软件时,默认将软件安装在了/usr/share/下,所以你的phpmyadmin在/usr/share下可以找到

所以,咱们必须建立一个软连接,使得第三步中显示的文件和/var/www/html下的某个文档链接起来,回到/var/www/html,输入一下代码

sudo ln -s /usr/share/phpmyadmin phpmyadmin

就行了!

2,关于Centos7下安装

  
  

一、安装Apache

yum install httpd

安装成功后,Apache操作命令:

systemctl start httpd      //启动apache
systemctl stop httpd       //停止apache
systemctl restart httpd    //重启apache
systemctl enable httpd     //设置apache开机启动

异常处理
我再阿里云上配置并出现启动Apache后无法访问的问题,但是一般服务器访问Apache可能需要如下操作:
(1)在防火墙中开放80端口
现在需要将 http 服务加入防火墙以允许外部访问,

firewall-cmd --add-service=http --permanent

–permanent 参数表示这是一条永久防火墙规则,如果不加则重启系统后就没有这条规则了。

而对于自定义的端口(如81),也需要添加防火墙规则,

firewall-cmd --zone=public --add-port=81/tcp --permanent

重启 Firewalld 使该规则生效,

systemctl restart firewalld

(2)关闭SELINUX

vi /etc/selinux/config

注释掉如下两句,添加最后一项

\#SELINUX=enforcing #注释掉
\#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加

:wq! 保存退出

输入如下命令

setenforce 0 #使配置立即生效

二、 安装MariaDB (MySQL的一个开源分支)

yum install mariadb mariadb-server

MariaDB安装成功后,需要配置MySQL的root密码,此外,备注一下启动关闭MariaDB的常用命令

systemctl start mariadb     //启动MariaDB
systemctl stop mariadb      //停止MariaDB
systemctl restart mariadb   //重启MariaDB
systemctl enable mariadb    //设置开机启动

设置root账户密码

mysql_secure_installation

Enter current password for root (enter for none):
Set root password? [Y/n]

点击回车然后提示是否设置root账号密码,输入y

New password:
Re-enter new password:
Password updated successfully!

提示输入新密码和重复输入新密码,重复输入两次后,出现更新密码成功提示。

然后一路输入y就可以。

Remove anonymous users? [Y/n] y
... Success!

Disallow root login remotely? [Y/n] y
... Success!

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reload privilege tables now? [Y/n] y
... Success!

Thanks for using MariaDB!

设置root密码后,重启MariaDB生效

systemctl restart mariadb.service

测试访问数据库:

mysql -uroot -p

然后输入密码,登录成功后显示如下:

Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
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;

退出命令:

exit;

三、安装PHP以及PHP拓展

yum install php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

安装完成后,重启Apache服务器

systemctl restart httpd.service

测试PHP安装结果

vi /var/www/html/index.php 

输入如下内容

<?php
  phpinfo();
?>

输入:wq! 保存退出
在浏览器中输入服务器地址,查看是否可以看到:

四、安装phpmyadmin

使用yum安装phpmyadmin

yum install phpmyadmin php-mcrypt

phpMyAdmin 的默认安装目录是 /usr/share/phpMyAdmin,同时会在 Apache 的配置文件目录中自动创建虚拟主机配置文件 /etc/httpd/conf.d/phpMyAdmin.conf(区分大小写)。默认情况下,CentOS 7上的phpMyAdmin只允许从回环地址(127.0.0.1)访问。为了能远程连接,你需要改动它的配置。

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

修改配置文件,如下:

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
      # Require ip 127.0.0.1  #注释掉
      # Require ip ::1   #注释掉
      Require all granted   #新添加
     </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/setup/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
      #Require ip 127.0.0.1  #注释掉
      #Require ip ::1   #注释掉
      Require all granted   #新添加
     </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>

然后重启Apache服务器

systemctl restart httpd

然后就可以通过浏览器访问http://服务器ip地址/phpmyadmin访问

访问phpmyadmin页面

作者:TyiMan
链接:https://www.jianshu.com/p/bc14ff0ab1c7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值