一、安装nginx
以下命令均在root权限下执行,普通用户可通过su
命令切换
1.安装依赖
yum install gcc-c++ yum install pcre pcre-devel yum install openssl openssl-devel
2.下载源码
wget http://nginx.org/download/nginx-1.8.1.tar.gztar -zxvf nginx-1.8.1.tar.gzcd nginx-1.8.1
3.创建nginx用户
useradd -M -s /sbin/nologin nginx
-M 表示不创建home目录
-s 指定shell为不登录
4.配置编译
./configure --prefix=/usr/local/nginx \--with-http_stub_status_module \--with-http_ssl_module \--user=nginx \--group=nginxmake && make install
5.启动nginx
进入nginx的安装目录,即上面--prefix参数指定的
cd /usr/local/nginx sbin/nginx
打开浏览器即可访问了。
访问地址为本地地址
访问地址为:192.168.253.131
注意如果是远程浏览器不能访问,可能是防火墙的80端口默认没有开启,需要开启这个端口。
打开/etc/sysconfig/iptables,在-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
下面添加:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存,并执行:service iptables restart
6.启动关闭nginx命令
检查nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t
启动nginx
/usr/local/nginx/sbin/nginx
关闭nginx
/usr/local/nginx/sbin/nginx -s stop 或 pkill nginx
重启nginx
/usr/local/nginx/sbin/nginx -s reload
7.添加系统服务
当然也可以将可以将nginx作为系统服务管理,将下面启动脚本(参考:nginx.com)加入/etc/init.d/nginx中,赋予可执行权限。
#!/bin/sh## nginx - this script starts and stops the nginx daemin## chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# pidfile: /usr/local/nginx/logs/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 0 nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { configtest || return $? stop start }reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? 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" in start) 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 2esac
然后,可以使用如下命令:
service nginx {start|stop|status|restart|reload|configtest}
也可以将nginx服务加入开机启动:
chkconfig nginx on
二、安装mysql
1.安装依赖
yum install make cmake gcc-c++ bison-devel ncurses-devel
有的依赖包可能已经安装过了,可以用rpm -qa package-name
查询是否已经安装。
2.下载源码
wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.54.tar.gztar -zxvf mysql-5.5.49.tar.gzcd mysql-5.5.49.tar.gz
3.创建mysql用户
useradd -M -s /sbin/nologin mysql
4.配置编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_DATADIR=/usr/local/mysql/data \-DSYSCONFDIR=/etc \-DWITH_MYISAM_STORAGE_ENGINE=1 \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_MEMORY_STORAGE_ENGINE=1 \-DWITH_READLINE=1 \-DENABLE_LOCAL_INFILE=1 \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DEXTRA_CHARSETS=all \-DWITH_PARTITION_STORAGE_ENGINE=1 \-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \-DMYSQL_TCP_PORT=3306 \-DMYSQL_USER=mysqlmake && make install
5.配置mysql
修改/usr/local/mysql权限
chown -R mysql:mysql /usr/local/mysql
复制配置文件,复制前注意查看/etc目录下是否有my.cnf文件,如果有,先将其备份为其他文件名
cp /etc/my.cnf /etc/my.cnf.backupcd /usr/local/mysqlcp support-files/my-small.cnf /etc/my.cnf
执行初始化配置脚本,创建系统自带的数据库和表
chmod +x scripts/mysql_install_db scripts/mysql_install_db --basedir=/usr/local/mysql \--datadir=/usr/local/mysql/data \--user=mysql
6.添加系统服务
复制服务脚本到/etc/init.d目录,并设置开机启动
cp support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig mysql on#启动mysqlservice mysql start#停止mysqlservice mysql stop#重启mysqlservice mysql restart
7.命令行登陆mysql
首先将mysql登陆命令的路径加入PATH中,修改/etc/profile文件,在文件末尾加上:
PATH=/usr/local/mysql/bin:$PATHexport PATH
关闭文件,然后执行source /etc/profile
让配置立即生效
登陆mysql并修改root用户密码
mysql -u rootset password = password(‘123456‘);
下次登陆时就需要密码。
mysql -u root -p
注意
如果远程登陆mysql出错,可能是防火墙的3306端口默认没有打开。同上面nginx开启防火墙80端口一样,在/etc/sysconfig/iptables文件加入:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
保存,并执行:service iptables restart
三、安装php
1.安装依赖
yum install libxml2-devel openssl-devel libcurl-devel gd-devel libpng-devel freetype-devel libjpeg-devel zlib-devel
解决yum无法安装libiconv库
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar -zxvf libiconv-1.14.tar.gz cd libiconv-1.14./configure --prefix=/usr/local/libiconv make && make install
另外,centos默认的yum源里没有libmcrypt-devel,这里使用阿里云的yum源安装。
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repoyum install libmcrypt-devel mcrypt mhash
2.下载源码
wget -O php-5.6.19.tar.gz http://cn2.php.net/get/php-5.6.19.tar.gz/from/this/mirrortar -zxvf php-5.6.19.tar.gzcd php-5.6.19
3.配置编译
./configure --prefix=/usr/local/php \--with-mysql=mysqlnd \--with-mysqli=mysqlnd \--with-pdo-mysql=mysqlnd \--enable-opcache \--enable-fpm \--with-iconv-dir=/usr/local/libiconv \--with-freetype-dir \--with-jpeg-dir \--with-png-dir \--with-zlib \--with-libxml-dir=/usr \--enable-xml \--disable-rpath \--enable-bcmath \--enable-shmop \--enable-sysvsem \--enable-inline-optimization \--with-curl \--with-curlwrappers \--enable-mbregex \--with-mcrypt \--with-gd \--enable-gd-native-ttf \--with-openssl \--with-mhash \--enable-pcntl \--enable-sockets \--with-xmlrpc \--enable-zip \--enable-soapmake && make install
这里特别说明下mysqlnd,mysqlnd是php官方自带的mysql驱动,这样安装php不需要提前安装mysql了。像--with-mysql=/usr/local/mysql
这样使用的是mysql官方自带的mysql驱动,需要提前安装mysql。
4.启动php
首先将php命令路径加入PATH中,修改/etc/profile文件,在文件末尾加上:
PATH=/usr/local/php/bin:$PATHexport PATH
关闭文件,然后执行source /etc/profile
让配置立即生效
然后复制源码目录中的php.ini-development到/usr/local/php/lib/php.ini
cp php.ini-development /usr/local/php/lib/php.ini
5.启动php-fpm并添加系统服务
PHP-FPM是一个PHP FastCGI管理器,主要用于配合web服务器处理php文件。
php-fpm命令位于/usr/local/php/sbin目录下,启动脚本在源代码目录的sapi/fpm子目录下。复制php-fpm启动脚本到/etc/init.d/php-fpm,设置开机启动。
复制/usr/local/php/etc/php-fpm.conf.default到/usr/local/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm chkconfig php-fpm on cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf#启动php-fpmservice php-fpm start#停止php-fpmservice php-fpm stop#重启php-fpmservice php-fpm restart
四、测试
为了让nginx能够处理php,需要修改nginx的配置文件(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;#}
修改为:
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
在/usr/local/nginx/html目录下创建一个test.php文件,内容如下:
<?php$con = mysqli_connect("127.0.0.1", "root", "123456");if (!$con) { die("Could not connect:" . mysqli_error($con)); }if (mysqli_query($con, ‘create database zhou‘)) { echo ‘Database created!‘; } else { echo ‘Database created error:‘ , mysqli_error($con); } mysqli_close($con);
打开浏览器访问:127.0.0.1/test.php。
登陆mysql,查看数据库zhou是否创建成功,如果创建成功表明LNMP环境搭建完成。