CentOS下LAMP环境的搭建

CentOS下LAMP环境的搭建

系统环境:CentOS 7.5.1804


1. 安装Apache

[root@mycentos ~]# yum -y install httpd httpd-devel

  设置Apache开机启动

[root@mycentos ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

  启动Apache服务

[root@mycentos ~]# systemctl start httpd

  查看服务状态

[root@mycentos ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2018-06-15 21:41:18 CST; 2min 15s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1573 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
       ├─1573 /usr/sbin/httpd -DFOREGROUND
       ├─1574 /usr/sbin/httpd -DFOREGROUND
       ├─1575 /usr/sbin/httpd -DFOREGROUND
       ├─1576 /usr/sbin/httpd -DFOREGROUND
       ├─1577 /usr/sbin/httpd -DFOREGROUND
       └─1578 /usr/sbin/httpd -DFOREGROUND

6月 15 21:40:53 mycentos systemd[1]: Starting The Apache HTTP Server...
6月 15 21:41:08 mycentos httpd[1573]: AH00558: httpd: Could not reliably determine the server's fully qualifie...essage
6月 15 21:41:18 mycentos systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

  防火墙开启80和443端口

[root@mycentos ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@mycentos ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@mycentos ~]# firewall-cmd --reload
success

  确认80端口已经处于监听状态

[root@mycentos ~]# netstat -lnp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      1573/httpd

  浏览器访问测试
这里写图片描述

2. 安装MySQL

  因为CentOS的软件源中默认没有MySQL,为了方便一点,这里我选用MariaDB来代替。MariaDB是在MySQL被Oracle公司收购后,由MySQL之父主导开发的MySQL分支,用起来其实和MySQL没区别。

[root@mycentos ~]# yum -y install mariadb mariadb-server mariadb-libs mariadb-devel

  开启MariaDB服务,将其设置为开机启动并查看其服务状态

[root@mycentos ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@mycentos ~]# systemctl start mariadb
[root@mycentos ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2018-06-15 22:20:40 CST; 13s ago
  Process: 1883 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1804 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 1882 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
       ├─1882 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
       └─2044 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --...

6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: MySQL manual for more instructions.
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: Please report any problems at http://mariadb.org/jira
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: The latest information about MariaDB is available at http...rg/.
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: You can find additional information about the MySQL part at:
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: http://dev.mysql.com
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: Consider joining MariaDB's strong and vibrant community:
6月 15 22:20:38 mycentos mariadb-prepare-db-dir[1804]: https://mariadb.org/get-involved/
6月 15 22:20:38 mycentos mysqld_safe[1882]: 180615 22:20:38 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
6月 15 22:20:38 mycentos mysqld_safe[1882]: 180615 22:20:38 mysqld_safe Starting mysqld daemon with databases f...mysql
6月 15 22:20:40 mycentos systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

  确认3306端口已经处于监听状态

[root@mycentos ~]# netstat -lnp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2044/mysqld

  进行数据库安全设置,数据库初始 root 密码为空,因此这里直接敲回车即可

[root@mycentos ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

  设置 root 密码

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

  是否删除匿名用户,这里选是

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

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

  是否禁止远程登录 root 用户,这里我选择了否,安全起见其实选是比较好

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

  删除测试数据库

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

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

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

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

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

  登陆数据库测试

[root@mycentos ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

3. 安装PHP

[root@mycentos ~]# yum -y install php

  为PHP添加MySQL支持

[root@mycentos ~]# yum -y install php-mysql

  最后测试一下PHP是否成功安装,在 /var/www/html 目录下创建文件 info.php ,编辑其内容为

<?php
    phpinfo();
?>

  重启Apache服务器

[root@mycentos ~]# systemctl restart httpd

  浏览器访问刚刚创建的页面,如果是下图这样就说明安装成功了
这里写图片描述

猜你喜欢

转载自blog.csdn.net/dyminas/article/details/80710505