搭建LNMP环境(基于最小化安装CentOS 6.5)


本文主要说明在单台服务器上手动安装LNMP环境的操作步骤本文档使用的系统版本可能与您的实际使用版本不同,您可以根据实际情况选择相应版本。
一、本文档LNMP环境版本说明:
OS:最小化安装CentOS 6.5Nginxngi开发云主机域名nx-1.10.2.tar.gzMySQLmysql-5.6.24.tar.gzPHPphp-5.6.23.tar.bz2
二、搭建LNMP环境基本步骤
1.准备编译环境2.安装nginx3.安装mysql4.安装php-fpm5.测试访问1、最小化安装CentOS6.5(步骤略)# cat /etc/redhat-release CentOS release 6.5 (Final)2、关闭SELINUX# sed -i ‘s/SELINUX=.*/SELINUX=disabled/g’ /etc/selinux/config# setenforce 03、配置防火墙,开启80端口# vi /etc/sysconfig/iptables-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT# service iptables restart1、添加运行nginx服务进程的用户# groupadd -r nginx # useradd -r -g nginxnginx2、下载源码包解压编译# yum -y install wget gccgcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel# wget http://nginx.org/download/nginx-1.10.2.tar.gz# tar xvf nginx-1.10.2.tar.gz-C/usr/local/src# cd /usr/local/src/nginx-1.10.2# ./configure –prefix=/usr/local/nginx –sbin-path=/usr/sbin/nginx –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/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fcgi –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi –http-scgi-temp-path=/var/tmp/nginx/scgi –user=nginx –group=nginx –with-pcre –with-http_v2_module –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-mail –with-mail_ssl_module –with-file-aio –with-ipv6 –with-http_v2_module –with-threads –with-stream –with-stream_ssl_module# make && make install# mkdir -pv /var/tmp/nginx/client3、添加SysV启动脚本# vi /etc/init.d/nginx#!/bin/sh # # nginx – this script starts and stops the nginx daemon # # chkconfig: – 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions# Source networking configuration. . /etc/sysconfig/network# Check that networking is up. [ “$NETWORKING” = “no” ] && exit 0nginx=”/usr/sbin/nginx”prog=$(basename $nginx)NGINX_CONF_FILE=”/etc/nginx/nginx.conf”[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $”Starting $prog: ” daemon $nginx -c $NGINX_CONF_FILEretval=$?echo [ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $”Stopping $prog: ” killproc $prog -QUITretval=$?echo [ $retval -eq 0 ] && rm -f $lockfilereturn $retvalkillall -9 nginx}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $”Reloading $prog: ” killproc $nginx -HUPRETVAL=$?echo }force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case “$1” instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}” exit 2esac4、赋予脚本执行权限。# chmod +x /etc/init.d/nginx5、添加至服务管理列表,设置开机自启。# chkconfig –add nginx# chkconfignginx on6、启动服务。# service nginx start7、浏览器访问测试1、准备编译环境。# yum install ncurses-devel# yum install cmake2、准备mysql数据存放目录、添加运行mysql服务进程的用户。# mkdir -pv /data/mysql# groupadd -r mysql# useradd -r -g mysql -s /sbin/nologin mysql# id mysqluid=497(mysql) gid=498(mysql) groups=498(mysql)3、更改数据目录属主属组。# chown -R mysql:mysql /data/mysql4、解压编译在MySQL官网下载的稳定版源码包,这里使用的是5.6.24版本# wget https://downloads.mysql.com/archives/get/file/mysql-5.6.24.tar.gz# tar xvf mysql-5.6.24.tar.gz -C/usr/local/src# cd /usr/local/src/mysql-5.6.24# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci# make && make install5、修改安装目录的属组为mysql# chown -R mysql:mysql /usr/local/mysql/6、初始化数据库。# /usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/data/mysql –basedir=/usr/local/mysql7、拷贝配置文件和启动脚本。# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld# chmod +x /etc/init.d/mysqld# mv /etc/my.cnf /etc/my.cnf.bak //最小安装CentOS 6.5后,会在/etc目录下存在一个my.cnf,将此文件更名。# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf8、设置开机自动启动。# chkconfig mysqldon # chkconfig –add mysqld9、修改配置文件中的安装路径及数据目录存放路径。# echo -e “basedir = /usr/local/mysqlndatadir = /data/mysqln” >> /etc/my.cnf10、设置PATH环境变量。# echo “export PATH=$PATH:/usr/local/mysql/bin” > /etc/profile.d/mysql.sh # source /etc/profile.d/mysql.sh11、启动服务。# service mysqld start # mysql -h 127.0.0.1//登录验证MySQL12运行mysql安全工具,设置root账户密码、删除匿名用户、不允许root远程登录、删除测试库等。# /usr/local/mysql/bin/mysql_secure_installation13、(后续可选步骤)建立应用数据库app1_db、应用账户app1_user并授权。# mysql -h 127.0.0.1 -uroot -pmysql>create database app1_db;mysql>grant all privileges on app1_db.* to ‘app1_user’@’%’ identified by ‘app1_password’ with grant option;mysql> flush privileges;mysql> exitNginx本身不能处理PHP,作为web服务器,当它接收到请求后,不支持对外部程序的直接调用或者解析,必须通过FastCGI进行调用。如果是PHP请求,则交给PHP解释器处理,并把结果返回给客户端。PHP-FPM是支持解析php的一个FastCGI进程管理器。提供了更好管理PHP进程的方式,可以有效控制内存和进程、可以平滑重载PHP配置。1、安装依赖包。# yum install -y epel-release//安装epel扩展源,以便安装libmcrypt等。# yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel2、解压官网下载php源码包,编译安装。# wget http://cn.php.net/distributions/php-5.6.23.tar.bz2# tar xvf php-5.6.23.tar.bz2 -C /usr/local/src# cd /usr/local/src/php-5.6.23# ./configure –prefix=/usr/local/php –with-config-file-scan-dir=/etc/php.d –with-config-file-path=/etc –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –with-openssl -enable-xml –enable-sockets –enable-fpm –with-mcrypt –with-bz2# make && make install3、添加phpphp-fpm配置文件。# cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini# cd /usr/local/php/etc/# cp php-fpm.conf.default php-fpm.conf# sed -i ‘s@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@’ php-fpm.conf4、添加php-fpm启动脚本。# cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm# chmod +x /etc/init.d/php-fpm5、添加php-fpm至服务列表并设置开机自启。# chkconfig –add php-fpm # chkconfig php-fpm on6、启动服务。# service php-fpm start7、添加nginxfastcgi的支持,首先备份默认的配置文件。# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak# cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf编辑/etc/nginx/nginx.conf,添加php格式,类似如下:location / {root /usr/local/nginx/html;index index.php index.html index.htm;}取消以下内容前面的注释:location ~ .php$ {root /usr/local/nginx/html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;include fastcgi_params;}重新载入nginx的配置文件。# service nginx reload/usr/local/nginx/html/新建index.php的测试页面,内容如下。注:以下测试内容中数据库账号为root,密码为p@ssw0rd# cat index.php $conn=mysql_connect(‘127.0.0.1′,’root’,’p@ssw0rd‘);if ($conn){echo “LNMP platform connect to mysql is successful!”;}else{echo “LNMP platform connect to mysql is failed!”;}phpinfo();?>浏览器访问测试,如看到以下内容则表示LNMP环境搭建完成。

相关推荐: Windows Server 2012 R2 路由功能部署

通过部署路由服务器来实现内部网络之间跨网段的通信。1.更改计算机名称,确定,重启计算机2.设置双网卡的IP地址3.10.0网段设置IP地址4.11.0网段设置IP地址5.打开服务器管理器,选择添加角色和功能6.下一步7.下一步8.下一步9.选择远程访问10.下…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

Like (0)
Donate 微信扫一扫 微信扫一扫
Previous 04/02 16:43
Next 04/02 16:43