Linux建站必会web环境:centos7用yum搭建LAMP

实验环境:

[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

3.7编写一个网页脚本,打开网页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容