实验环境:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
1、安装aphe
1.1安装apache
[root@localhost ~]# yum install httpd httpd-devel
最后一行见基本报错解答
安装完成以后以后默认路径,配置文件路径在/etc/httpd/conf
httpd默认默认主页存放目录,[root@localhost html]# pwd /var/www/html
1.2启动apache服务
[root@localhost ~]# systemctl start httpd
1.3设置httpd服务开机启动
[root@localhost ~]# systemctl enable httpd
1.4查看服务状态
[root@localhost ~]# 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-07-13 10:02:03 CST; 2min 37s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2159 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─2159 /usr/sbin/httpd -DFOREGROUND
├─2161 /usr/sbin/httpd -DFOREGROUND
├─2162 /usr/sbin/httpd -DFOREGROUND
├─2163 /usr/sbin/httpd -DFOREGROUND
├─2164 /usr/sbin/httpd -DFOREGROUND
└─2165 /usr/sbin/httpd -DFOREGROUND
7月 13 10:02:03 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
7月 13 10:02:03 localhost.localdomain httpd[2159]: AH00558: httpd: Could not reliably d...ge
7月 13 10:02:03 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
1.5防火墙开启80端口
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --reload
success
1.6确认80端口监听中
[root@localhost ~]# netstat -tulp
[root@localhost ~]# yum -y install net-tools
[root@localhost ~]# netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN 999/sshd
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN 1200/master
tcp6 0 0 [::]:http [::]:* LISTEN 1432/httpd
tcp6 0 0 [::]:ssh [::]:* LISTEN 999/sshd
tcp6 0 0 localhost:smtp [::]:* LISTEN 1200/master
udp 0 0 localhost:323 0.0.0.0:* 631/chronyd
udp 0 0 0.0.0.0:bootpc 0.0.0.0:* 800/dhclient
udp6 0 0 localhost:323 [::]:* 631/chronyd
1.8 查服务器IP
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:41:80:73 brd ff:ff:ff:ff:ff:ff
inet 192.168.85.139/24 brd 192.168.85.255 scope global noprefixroute dynamic ens33
valid_lft 1632sec preferred_lft 1632sec
inet6 fe80::55c:5495:cdff:d09d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
补充:linux上安装apache以及httpd.conf基本配置
httpd.conf配置请参照下面
#vi /etc/httpd/conf/httpd.conf
以下展示的是对默认值的修改。
#禁止显示apache版本号
ServerTokens ProductOnly
ServerSignature Off
#端口监听,我们将*改成了我们的独立ip
Listen *:80
#我们开启了两个模块,其他模块默认设置
mod_rewrite.so #开启.htaccess需要
mod_vhost_alias.so #设置虚拟机需要
#我们将ServerAdmin改成我们自己的邮箱。
ServerAdmin admin@1try10.com
#我们将ServerName导向固定ip,即将*改成我们的ip
ServerName *:80
#我们修改了DocumentRoot目录
DocumentRoot /***
#将Options属性改成FollowSymLinks
Options FollowSymLinks
#将AllowOverride属性改成ALL以支持.htaccess
AllowOverride ALL
#我们配置了虚拟机,我们把*更改成我们的ip
NameVirtualHost *:80
#虚拟机上拒绝了直接通过ip访问我们的站点,我们把*更改成我们的ip
<VirtualHost *:80>
ServerName *
<Location />
Order deny,allow
Deny from all
</Location>
</VirtualHost>
#我们将域名绑定到服务器,并将不带3www的域名301重定向到带www域名
<VirtualHost *:80>
ServerAdmin admin@1try10.com
DocumentRoot 一个目录
ServerName 1try10.com
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^1try10.com [NC]
RewriteRule ^/(.*)$ http://www.1try10.com/$1 [L,R=301]
</ifModule>
ErrorLog /var/log/1try10.com-error_log
CustomLog /var/log/1try10.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@1try10.com
DocumentRoot 一个目录
ServerName www.1try10.com
ErrorLog /var/log/1try10.com-error_log
CustomLog /var/log/1try10.com-access_log common
</VirtualHost>
#我们设定了gzip压缩
#gzip
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
DeflateCompressionLevel 5
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
</IfModule>
#TRACE和TRACK是用来调试web服务器连接的HTTP方式.支持该方式的服务器存在跨站脚本漏洞,所以我们关闭了它
TraceEnable off
2、安装mysql
2.1安装mysql
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel
已加载插件:fastestmirror, priorities
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.huaweicloud.com
软件包 1:mariadb-5.5.56-2.el7.x86_64 已安装并且是最新版本
软件包 1:mariadb-server-5.5.56-2.el7.x86_64 已安装并且是最新版本
软件包 1:mariadb-libs-5.5.56-2.el7.x86_64 已安装并且是最新版本
软件包 1:mariadb-devel-5.5.56-2.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# rpm -qa |grep maria
mariadb-server-5.5.56-2.el7.x86_64
mariadb-5.5.56-2.el7.x86_64
mariadb-devel-5.5.56-2.el7.x86_64
mariadb-libs-5.5.56-2.el7.x86_64
2.2 开启mysql服务,并设置开机启动,检查mysql状态
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to
/usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# 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-07-13 09:49:49 CST; 12s ago
Main PID: 1684 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─1684 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─1846 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir...
7月 13 09:49:45 localhost.localdomain mariadb-prepare-db-dir[1606]: MySQL manual for more...
7月 13 09:49:45 localhost.localdomain mariadb-prepare-db-dir[1606]: Please report any pro...
7月 13 09:49:45 localhost.localdomain mariadb-prepare-db-dir[1606]: The latest informatio...
7月 13 09:49:46 localhost.localdomain mariadb-prepare-db-dir[1606]: You can find addition...
7月 13 09:49:46 localhost.localdomain mariadb-prepare-db-dir[1606]: http://dev.mysql.com
7月 13 09:49:46 localhost.localdomain mariadb-prepare-db-dir[1606]: Consider joining Mari...
7月 13 09:49:46 localhost.localdomain mariadb-prepare-db-dir[1606]: https://mariadb.org/g...
7月 13 09:49:46 localhost.localdomain mysqld_safe[1684]: 180713 09:49:46 mysqld_safe Log....
7月 13 09:49:46 localhost.localdomain mysqld_safe[1684]: 180713 09:49:46 mysqld_safe Sta...l
7月 13 09:49:49 localhost.localdomain systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 1846/mysqld
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN 999/sshd
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN 1200/master
tcp6 0 0 [::]:http [::]:* LISTEN 1432/httpd
tcp6 0 0 [::]:ssh [::]:* LISTEN 999/sshd
tcp6 0 0 localhost:smtp [::]:* LISTEN 1200/master
udp 0 0 localhost:323 0.0.0.0:* 631/chronyd
udp 0 0 0.0.0.0:bootpc 0.0.0.0:* 800/dhclient
udp6 0 0 localhost:323 [::]:* 631/chronyd
2.3 数据库安全设置
[root@localhost ~]# 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): *直接回车
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...
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!
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!
2.4 登陆数据库测试
[root@localhost ~]# mysql -uroot -p
Enter password: #输入刚刚设置的密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
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.00 sec)
MariaDB [(none)]> exit;
Bye
3、安装PHP
3.1 安装php
[root@localhost ~]# yum -y install php
[root@localhost ~]# rpm -ql php
/etc/httpd/conf.d/php.conf
/etc/httpd/conf.modules.d/10-php.conf
/usr/lib64/httpd/modules/libphp5.so
/usr/share/httpd/icons/php.gif
/var/lib/php/session
3.2 将php与mysql关联起来
[root@localhost ~]# yum install php-mysql
[root@localhost ~]# rpm -ql php-mysql
/etc/php.d/mysql.ini
/etc/php.d/mysqli.ini
/etc/php.d/pdo_mysql.ini
/usr/lib64/php/modules/mysql.so
/usr/lib64/php/modules/mysqli.so
/usr/lib64/php/modules/pdo_mysql.so
3.3 安装常用PHP模块
[root@localhost ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring phpsnmp
php-soap curl curl-devel php-bcmath
3.4 测试PHP
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# vi info.php
<?php
phpinfo();
?>
~
~
~
~
~
~
~
~
:wq
3.5重启apache服务器
[root@localhost html]# systemctl restart httpd