常见的几种Web环境架构:
LNMP: Linux+Nginx+Mysql+php (异步非阻塞,适合高并发环境,简洁占用资源较少)
LAMP: Linux+Apache+Mysql+php (阻塞型,适合稳定且拓展模块需求多环境)
LNAMP :(Linux + Nginx+ MySQL+ PHP+ Apache):Nginx处理静态,Apache(mod_php)处理动态PHP
LNMT:(Linux + Nginx+ MySQL+ Tomcat):Nginx处理静态,Tomcat(JDK)处理JAVA
一般搭建LNMP可以有很多种方法,用脚本一键自动安装,虽然简单快捷,但是对于后期维护及可控性不强;而源码编译虽自定义性较强但是安装复杂且需要解决依赖关系;Redhat/Centos 用户我建议如果没有特殊需求,使用YUM的方式手动安装偏于后期的维护升级。注:全程关闭防火墙和Selinux。
1、镜像源切换
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all yum makecache yum update
2、安装Nginx
2.1yum安装
Centos默认源中没有nginx的,安装了阿里云或者fedora的第三方源可以直接执行yum install nginx ;
vim /etc/yum.repos.d/nginx.repo
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
yum list nginx yum list |grep nginx
安装成功后,就可以直接安装nginx了
yum -y install nginx
安装的就是Nginx官网的最新版本
systemctl start Nginx
可以使用curl命令查看是否安装成功
#curl 127.0.0.1
如果安装成功的话,就会看到输出一个HTML的一个反馈
开机启动设置
systemctl enable nginx
2.2源码编译安装
安装依赖
yum -y install gcc gcc-c++ glibc automake autoconf libtool make pcre pcre-devel zlib zlib-devel openssl openssl-devel GeoIP GeoIP-devel GeoIP-data lua-devel ibxml2 libxml2-dev libxslt-devel gd-devel
创建www用户和组,和php共用同一组和用户
groupadd www useradd -g www -s /sbin/nologin -M www #加入www组并指定无登录shell和主目录
Nginx官网下载二进制包并解压
wget -c http://nginx.org/download/nginx-1.14.2.tar.gz tar -zxvf nginx-1.14.2.tar.gz cd nginx-1.14.2
配置编译安装
./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=www \ --group=www \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --with-http_slice_module \ --with-http_v2_module \ --with-threads \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-mail \ --with-mail_ssl_module \ --with-compat \ --with-file-aio
编译安装
make && make install
Nginx 命令
/usr/sbin/nginx #启动
nginx -t #测试配置文件
nginx -s reload #平滑重启
nginx -s stop #退出
使用systemctl管理Nginx服务
#编辑nginx服务 vim /lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target #只有就可以使用systemctl start | stop | restart |reload |enable nginx.service 管理nginx服务了
Mysql安装
官网:http://dev.mysql.com/downloads/repo/yum/
安装官方源
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
查看5.7版本是否已经启用
#yum repolist all | grep mysql
如果没有启用的话,我们可以修改源文件
vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community] name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
修改完成之后查看可用的版本
#yum repolist enabled | grep mysql
如果看到5.7版本启用了之后就可以安装MySql了
yum -y install mysql-community-server
安装完成之后,就可以启动mysql了
#systemctl start mysqld
查看MySql的启动状态
#systemctl status mysqld
开机启动设置
#systemctl enable mysqld
MySql安装完成之后会在LOG文件(/var/log/mysqld.log)中生成一个root的默认密码
#grep ‘temporary password’ /var/log/mysqld.log
2017-05-23T14:51:45.705458Z 1 [Note] A temporary password is generated for [email protected]: d&sqr7dcf7P_
最后执行一键安全设置:更改密码与关闭匿名登录等
#mysql_secure_installation
登录mysql
#mysql -u root -p
创建数据库与用户并赋权
1、create database 数据库名称
2、CREATE USER ‘jeffrey’@’localhost’ IDENTIFIED BY ‘mypass’;
3、GRANT ALL ON db1.* TO ‘jeffrey’@’localhost’;
安装PHP
1.安装官方源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
2、安装PHP7
yum install -y php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
3、安装php-fpm
yum install php70w-fpm php70w-opcache
注:如果要安装其他版本的php可以把数字70换成56或其他的版本数字
4、启动php-fpm
systemctl start php-fpm
5、开机启动设置
systemctl enable php-fpm
因为nginx对于php的处理是转发到php-fpm进程来处理的。所以需要更改下nginx的配置文件
修改 /etc/nginx/conf.d/default.conf
修改默认的 location 块,使其支持 .php 文件:
location / { root html; index index.php index.html index.htm; }
取消下面的注释并修改如下,启用fastcgi对php进行解析
location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
重启Nginx使修改生效:
systemctl restart nginx
最后查看下进程端口是否都正常。
#netstat -ntlp
注:Nginx和php-fpm的启动用户建议都统一修改成统一用户与组,如www;并给予相关权限。