自己学习一下怎么安装LAMP环境,新手一枚,有很多地方参考了别人的经验,有错误的地方望指正
1.安装Apache
[root@localhost src]# wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.33.tar.gz
[root@localhost src]# tar -xf httpd-2.4.33.tar.gz
编译安装httpd
[root@localhost src]# cd httpd-2.4.33
[root@localhost httpd-2.4.33]# ./configure
遇到了错误
checking for APR... no
configure: error: APR not found. Please read the documentation.
原因是缺少Apache运行库,需要安装APR和APR-UTIL
试过了yum安装,不知道为啥安装完还是报这个错,只好试试自己编译安装咯,下载地址在这
编译安装APR
[root@localhost src]# tar -xf apr-1.6.3.tar.gz
[root@localhost src]# cd apr-1.6.3
[root@localhost apr-1.6.3]# ./configure
又遇到一个错误
configure: error: no acceptable C compiler found in $PATH
原因是未安装gcc编译器,给装它一个
[root@localhost apr-1.6.3]# yum install -y gcc-c++
再来一遍,过了
[root@localhost apr-1.6.3]# ./configure
[root@localhost apr-1.6.3]# make && make install
轮到APR-UTIL
[root@localhost apr-1.6.3]# cd ../
[root@localhost src]# tar -xf apr-util-1.6.1.tar.gz
[root@localhost src]# cd apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure
错误还是无可避免滴
configure: error: APR could not be located. Please use the --with-apr option.
无法定位到APR的位置,需要带上 --with-apr 参数编译
[root@localhost apr-util-1.6.1]# ./configure --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
有报错了
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
缺少expat解析库,给装上
[root@localhost apr-util-1.6.1]# yum install -y expat-devel
继续
[root@localhost apr-util-1.6.1]# make && make install
继续编译安装httpd,出现了个新的错误
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
装上
[root@localhost httpd-2.4.33]# yum install -y pcre-devel
终于编译过了
[root@localhost httpd-2.4.33]# ./configure --enable-so
[root@localhost httpd-2.4.33]# make && make install
又爆出一堆错误
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/httpd-2.4.33/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/httpd-2.4.33/support'
make: *** [all-recursive] Error 1
google了一大圈发现各种说法都有,自己去看看Apache安装文档才知道哪里出错了,下载好APR和APR-UTIL库后把这两个安装文件分别解压到 /httpd_source_tree_root/srclib/apr
和 /httpd_source_tree_root/srclib/apr-util,然后再编译安装httpd
[root@localhost src]# cp -r apr-1.6.3 /httpd-2.4.33/srclib/apr
[root@localhost src]# cp -r apr-1.6.3 /httpd-2.4.33/srclib/apr-util
再次编译安装httpd
[root@localhost httpd-2.4.33]# ./configure --enable-so
[root@localhost httpd-2.4.33]# make && make install
安装完成后需要编辑一下Apache的配置文件
[root@localhost httpd-2.4.33]# vi /usr/local/apache2/conf/httpd.conf
找到
#ServerName www.example.com:80
修改为
ServerName 你的服务器ip:80
设置Apache为系统服务并开机自启动
[root@localhost httpd-2.4.33]# cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd
[root@localhost apache2]# vi /etc/init.d/httpd
在第二行加上
#chkconfig:2345 10 90
#description: Start and stops the Apache HTTP Server.
添加为系统服务并开机启动
[root@localhost bin]# chkconfig --add httpd
[root@localhost bin]# chkconfig httpd on
[root@localhost apache2]# chkconfig --list httpd
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动Apache
[root@localhost apache2]# systemctl start httpd.service
[root@localhost apache2]# ps -ef|grep httpd
root 22907 1 0 19:59 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 22908 22907 0 19:59 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 22909 22907 0 19:59 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 22910 22907 0 19:59 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
root 22993 1304 0 19:59 pts/0 00:00:00 grep --color=auto httpd
访问服务器ip地址出现了这个页面,ok
2.安装MySQL
CentOS7默认安装了mariadb,所以要干掉它
[root@localhost src]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost src]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost src]# rm /etc/my.cnf
我的虚拟机内存很小,就装个MySQL5.5来耍耍
[root@localhost src]# wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxvf mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# mv mysql-5.5.60-linux-glibc2.12-x86_64 /usr/local/mysql
切换到mysql目录,创建用户和组
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -g mysql mysql
[root@localhost mysql]# chown -R mysql:mysql ./
安装MySQL
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
似乎安装成功了
Installing MySQL system tables...
180717 15:56:02 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180717 15:56:02 [Note] /usr/local/mysql//bin/mysqld (mysqld 5.5.60-log) starting as process 2508 ...
OK
Filling help tables...
180717 15:56:03 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180717 15:56:03 [Note] /usr/local/mysql//bin/mysqld (mysqld 5.5.60-log) starting as process 2515 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql//bin/mysqladmin -u root password 'new-password'
/usr/local/mysql//bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
Alternatively you can run:
/usr/local/mysql//bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql/ ; /usr/local/mysql//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql//mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
配置mysql
[root@localhost src]# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
[root@localhost src]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
将以下内容添加到my.cnf 中的对应位置
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
interactive_timeout = 10
wait_timeout = 10
character-set-server = utf8
max_connections = 1000
[mysql]
default-character-set = utf8
设置自启动
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动mysql服务
[root@localhost mysql]# systemctl start mysqld
添加一下mysql全局变量
[root@localhost mysql]# vim /etc/profile
把这段添加到最后一行
export PATH=$PATH:/usr/local/mysql/bin
使配置生效
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/mysql/bin
登录mysql,还没设置密码,直接确认就好
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.60-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
设置密码
mysql> use mysql
mysql> update user set password=password('需要设置的密码') where user='root' and host='localhost';
mysql> flush privileges;
允许远程连接mysql,先重新登录一下mysql
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root用户密码' with grant option;
3.安装PHP
下载解压
[root@localhost src]# wget http://cn2.php.net/distributions/php-7.0.30.tar.gz
[root@localhost src]# tar -zxvf php-7.0.30.tar.gz
安装一些扩展库
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
编译
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip
报错了
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
yum源没有libmcrypt
[root@localhost php-7.0.30]# yum search libmcrypt
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* extras: mirror.lzu.edu.cn
* updates: mirror.lzu.edu.cn
Warning: No matches found for: libmcrypt
No matches found
自己下一个
[root@localhost src]# wget https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@localhost libmcrypt-2.5.8]# tar zxvf libmcrypt-2.5.8.tar.gz
[root@localhost libmcrypt-2.5.8]# cd libmcrypt-2.5.8
[root@localhost libmcrypt-2.5.8]# ./configure
[root@localhost libmcrypt-2.5.8]# make && make install
Go on!
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip
[root@localhost php-7.0.30]# make
[root@localhost php-7.0.30]# make test
[root@localhost php-7.0.30]# make install
4.LAMP环境配置
修改Apache运行的用户和组
先添加个wwwroot用户
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g wwwroot www
修改httpd.conf文件,找到这个地方将用户名修改成wwwroot
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User wwwroot
Group wwwroot
</IfModule>
配置Apache解析php
使Apache加载php模块
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
在上面这一块里面找一下有没有加上这段话,如果是php5就改成php5_module
LoadModule php7_module modules/libphp7.so
然后这个地方后面加上两段变成这样
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php5
这里,修改成这样
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
#
修改网站根目录为自己指定的目录
DocumentRoot "/usr/data/wwwroot"
<Directory "/usr/data/wwwroot">
保存,重启Apache生效配置
[root@localhost php-7.0.30]# ps -ef|grep httpd
root 27307 1 0 18:31 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
www 27308 27307 0 18:31 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
www 27309 27307 0 18:31 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
www 27312 27307 0 18:31 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
root 27393 1299 0 18:31 pts/0 00:00:00 grep --color=auto httpd
去网站根目录创建一个info.php文件看看
<?php
echo phpinfo();
访问一下
成功了