實驗環(huán)境:
[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查看服務狀態(tài)
[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端口監(jiān)聽中
[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
#端口監(jiān)聽,我們將*改成了我們的獨立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是用來調(diào)試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狀態(tài)
[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 數(shù)據(jù)庫安全設置
[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 登陸數(shù)據(jù)庫測試
[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關聯(lián)起來
[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