CentOS 7 学习(一) 配置LAMP和Nginx

CentOS 7 学习(一) 配置LAMP和Nginx
CentOS是RedHat Linux企业版的代码编译版本,属于比较通用的服务器Linux版本,据说Ubuntu Server更通用,呵呵,不过个人觉得也许CentOS稳定一些,就把公司的服务器架设为CentOS。
参考了http://www.jb51.net/article/26597.htm和http://www.111cn.net/sys/CentOS/63646.htm

1、安装
安装的版本为CentOS 7.0 X64版本,选择最小安装,不过增加了兼容程序和开发工具,因为这些是肯定需要的。
安装网络工具:yum install net-tools,
系统默认是DHCP模式的,公司的DHCP服务器没有每次针对同一台机器分配同一个IP,所以需要手工设定IP,步骤如下
vim /etc/sysconfig/network-scripts/ifcfg-enp1s0
增加和修改如下内容
BOOTPROTO="static" #默认为dhcp,改为static
IPADDR=192.168.1.12 #IP地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.1 #网关
DNS1=8.8.8.8
DNS2=8.8.4.4
重启电脑或者重启网络服务:systemctl restart network

2、配置防火墙(参考了https://fedoraproject.org/wiki/FirewallD/zh-cn和http://www.tuicool.com/articles/vMr6Vj)
系统默认为的防火墙为firewalld防火墙,可以禁用它启用iptables防火墙,不过既然是默认的,就试试看吧
firewalld可以以守护进程的方式运行,这样可以动态配置而不需要重启防火墙,静态配置就必须重启防火墙
其配置工具有图形界面的firewall-config和命令行的firewall-cmd
firewall的管理方式有点类似于Windows防火墙,有一个区域(zone)的概念,不同的区域对应不同的安全级别,可以通过切换区域来修改整体防火墙级别。
基本命令如下
firewall-cmd --get-zones #获得所有的区域级别
firewall-cmd --list-all-zones #获取所有的区域详细信息,结果类似如下
public (default, active)
interfaces: enp0s3
sources:
services: dhcpv6-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
这里显示的是默认的public区域,上面的命令输出很多,我截取了其中一段,可以看出这里支持的服务,默认支持dhcpv6-client和ssh
firewall-cmd --get-default-zone #显示默认区域
firewall-cmd --list-services #显示当前区域支持的服务,即防火墙允许的服务
firewall-cmd --get-services #显示支持的服务
#这里需要增加一些服务和端口
firewall-cmd --add-service=http
firewall-cmd --add-service=mysql
firewall-cmd --add-port=9000/tcp
firewall-cmd --add-port=808/tcp
查看效果
[root@centos-s1 ~]# firewall-cmd --list-services
dhcpv6-client http mysql ssh
[root@centos-s1 ~]# firewall-cmd --list-ports
9000/tcp 808/tcp
后面需要安装mysql,php-fpm,nginx,需要端口3306,9000,808
但这些只是暂时性的,重启防火墙之后就会消失,命令为firewall-cmd --reload,
也可以用命令查看
firewall-cmd --permanent --list-services
firewall-cmd --permanent --list-ports
永久性的需要增加--permanent参数
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=mysql
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --permanent --add-port=808/tcp
现在重启服务也不会丢失参数
默认区域定义在/etc/firewalld/firewalld.conf中
配置文件保存在/etc/firewalld/zones/public.xml,这代表默认区域是public
3、安装LAMP
1)Apache
sudo yum install httpd
systemctl start httpd.service
systemctl enable httpd.service #设置开机启动
2)安装Mariadb
yum install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
3)安装php
#mysql驱动程序采用php-mysqlnd
yum install graphviz-php php php-bcmath php-cli php-common php-dba php-devel php-embedded php-enchant php-fpm php-gd php-intl php-ldap php-mbstring php-mysqlnd php-odbc php-pdo php-pear.noarch php-pgsql php-process php-pspell php-recode php-snmp php-soap php-xml php-xmlrpc rrdtool-php uuid-php php-pecl-memcache
下载内容有点多,46M
4)重启apache,systemctl restart httpd.service
vim /var/www/html/info.php
<?php
phpinfo();
?>
在浏览器中输入http://localhost/info.php,就可以看到效果了
4、安装phpmyadmin
1)修改mysql的root密码,因为phpmyadmin不允许空密码登录
mysql -uroot #默认情况下root密码为空
MariaDB [(none)]> use mysql;
Database changed
MariaDB [mysql]> select host,user,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| centos-s1 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| centos-s1 | | |
+-----------+------+----------+
6 rows in set (0.00 sec)

MariaDB [mysql]> update user set password=PASSWORD('stone123') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2)修改Apache,
vim /etc/httpd/conf/httpd.conf
修改为
DirectoryIndex index.html index.php #增加index.php
3)下载phpmyadmin
yum install wget unzip
wget http://nchc.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/4.2.7/phpMyAdmin-4.2.7-all-languages.zip
unzip phpMyAdmin-4.2.7-all-languages.zip
mv phpMyAdmin-4.2.7-all-languages phpMyAdmin
mkdir /work
cp /root/phpMyAdmin /work/
注意一点,需要将SELINUX关掉,需要重启机器
vim /etc/selinux/config
SELINUX=disabled
修改Apache配置文件
vim /etc/httpd/conf/httpd.conf
Alias /phpmyadmin /work/phpMyAdmin

<Directory "/work/phpMyAdmin">
Options Indexes
AllowOverride All
Require all granted
</Directory>

注意这里的Apache版本为2.4,和2.2有一些不同
http://localhost/phpmyadmin
5、安装Nginx
wget http://nginx.org/download/nginx-1.7.3.tar.gz
tar xvf nginx-1.7.3.tar.gz
./configure
#可能会缺少一些包,需要yum安装,如zlib,yum install zlib-devel
#这个命令会提示Nginx软件的安装位置,默认安装在/usr/local/nginx
启动命令为/usr/local/nginx/sbin/nginx
为方便起见,建立符号链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
这样就可以直接使用nginx命令启动了,不过nginx的默认启动端口为80,我们已经安装了Apache,需要修改端口
vim /usr/local/nginx/conf/nginx.conf
修改listen 80;为 listen 808;
nginx #启动
没有返回信息,说明启动正常
http://localhost:808 可以看到欢迎页面
配置php支持
修改nginx.conf,去掉注释,如下
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
启动php-cgi程序,php-fpm &
nginx默认住目录为/usr/local/nginx/html,增加info.php文件
<?php
phpinfo();
?>
访问http://localhost:808/info.php

配置Nginx作为Apache的代理,编辑nginx.conf,将下列注释取消
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
这代表扩展名为php的文件,会代理到本机的80端口
测试文件
vim /var/www/html/test.php
<?php
echo "Hello World!";
?>
浏览器里测试http://localhost:808/test.php
证明代理成功
测试:http://localhost:808/phpmyadmin/index.php
发现可以访问,不过要注意要把fastcgi模式的设置注释掉,否则会出现不正常的现象,很多图片没法访问
不过要注意,如果直接访问http://localhost:808/phpmyadmin,会出现错误,因为没有指定php扩展名,系统无法代理。
至于更详细的代理,明天再研究。

posted @ 2014-08-02 23:16  stone-fly  阅读(2192)  评论(0编辑  收藏  举报