这是之前在学习、工作过程中整理的笔记,完整的记录了以源码方式编译、配置 LNMP 环境的步骤,虽然也小范围用于生产环境的服务构建,但建议只把它拿来学习用,因为有些细节和优化项并不完善。
生产环境,强烈建议使用以下两个相对成熟、稳定的脚本工具:
1. 准备篇 1.1. 推荐目录结构 1 ➜ mkdir -p /lnmp/server /lnmp/www /lnmp/log/mysql /lnmp/log/nginx /lnmp/log/php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /lnmp/ ├── log │ ├── mysql │ ├── nginx │ └── php ├── server │ ├── mysql -> /lnmp/server/mysql-5.x.xx │ ├── mysql-5.x.xx │ ├── nginx -> /lnmp/server/nginx-1.4.4 │ ├── nginx-1.4.4 │ ├── php -> /lnmp/server/php-5.x.xx │ └── php-5.x.xx ├── info.log └── www
1 2 3 4 ➜ chown -R www:www /lnmp/log ➜ chmod -R 755 /lnmp/log ➜ chown -R www:www /lnmp/www ➜ chmod -R 755 /lnmp/www
1.2. 下载源码包 1 2 3 4 5 6 7 ➜ mkdir -p /root/src && cd /root/src ➜ wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.50-linux2.6-x86_64.tar.gz ➜ wget http://nginx.org/download/nginx-1.6.3.tar.gz ➜ wget http://cn2.php.net/distributions/php-5.5.36.tar.gz
1.3. 防火墙开放80/3306端口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ➜ /etc/init.d/iptables status ➜ vim /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 <------- 开放80 -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT <------- 开放3306 -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT ➜ /etc/init.d/iptables restart
1.4. 关闭 SELinux 1 2 3 ➜ sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config ➜ shutdown -r now
1.5. 安装编译时所需依赖 1 ➜ yum -y install gcc gcc-c++ kernel-devel
2. 安装 MySQL 2.1. 创建用户 1 2 3 4 5 6 7 8 9 10 11 ➜ groupadd mysql ➜ useradd -s /sbin/nologin -g mysql -M mysql ➜ useradd mysql -s /sbin/nologin -M ➜ tail -1 /etc/passwd mysql:x:501:501::/home/mysql:/sbin/nologin ➜ id mysql uid=501(mysql) gid=501(mysql) groups =501(mysql)
2.2. 解压、配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ➜ cd /root/src ➜ tar -zxvf mysql-5.5.50-linux2.6-x86_64.tar.gz ➜ mv mysql-5.5.50-linux2.6-x86_64 /lnmp/server/mysql-5.5.50 && cd /lnmp/server ➜ ls -l ➜ ln -s /lnmp/server/mysql-5.5.50/ mysql ➜ ls -l ➜ cd mysql ➜ ls -l support-files/*.cnf ➜ /bin/cp support-files/my-large.cnf /etc/my.cnf
2.3. 初始化数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ➜ mkdir -p /data/mysql ➜ chown -R mysql:mysql /data/mysql ➜ ./scripts/mysql_install_db --basedir=/lnmp/server/mysql --datadir=/data/mysql --user=mysql ... Installing MySQL system tables... 160529 18:23:19 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33182 ... OK Filling help tables... 160529 18:23:20 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33189 ... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system ...
2.4. 修改设置并启动服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ➜ cp ./support-files/mysql.server /etc/init.d/mysqld ➜ chmod 755 /etc/init.d/mysqld ➜ sed -i 's#/usr/local/mysql#/lnmp/server/mysql#g' /lnmp/server/mysql/bin/mysqld_safe /etc/init.d/mysqld ➜ vim /etc/init.d/mysqld basedir=/lnmp/server/mysql datadir=/data/mysql ➜ /etc/init.d/mysqld start Starting MySQL... SUCCESS! ➜ netstat -tlunp|grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33457/mysqld
2.5. 设置开机自启动 1 2 3 4 ➜ chkconfig --add mysqld ➜ chkconfig mysqld on ➜ chkconfig --list|grep mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.6. 配置MySQL命令全局使用 1 2 3 4 5 6 7 8 9 10 ➜ echo 'export PATH=/lnmp/server/mysql/bin:$PATH' >>/etc/profile ➜ tail -1 /etc/profile export PATH=/lnmp/server/mysql/bin:$PATH ➜ source /etc/profile ➜ echo $PATH /lnmp/server/mysql/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/whoru/bin
2.7. 登录测试,更改默认管理员密码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ➜ mysql Welcome to the MySQL monitor. Commands end with ; or \g. ..... Type 'help;' or '\h' for help . Type '\c' to clear the current input statement. mysql>quit; ➜ mysqladmin -u root password 'whoru123' ➜ mysql ERROR 1045 (28000): Access denied for user 'root' @'localhost' (using password: NO) ➜ mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ..... mysql>
2.8. 清理没有用的用户及库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | web1 | | root | web1 | +------+-----------+ 6 rows in set (0.00 sec) mysql> drop user "root" @"::1" ; Query OK, 0 rows affected (0.00 sec) mysql> drop user "" @"localhost" ; Query OK, 0 rows affected (0.00 sec) mysql> drop user "" @"web1" ; Query OK, 0 rows affected (0.00 sec) mysql> drop user "root" @"web1" ; Query OK, 0 rows affected (0.01 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
3. 安装 Nginx 3.1. 安装依赖包 1 2 3 4 5 6 7 8 ➜ rpm -qa pcre* openssl* zlib* zlib-1.2.3-29.el6.x86_64 openssl-1.0.1e-48.el6_8.1.x86_64 pcre-7.8-7.el6.x86_64 ➜ yum -y install pcre-devel openssl-devel zlib-devel
3.2. 添加用户 1 ➜ useradd www -s /sbin/nologin -M
3.3. 安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ➜ cd /root/src ➜ tar -zxvf nginx-1.6.3.tar.gz && cd nginx-1.6.3 ➜ ./configure \ --prefix=/lnmp/server/nginx-1.6.3 \ --error-log-path=/lnmp/log/nginx/error.log \ --http-log-path=/lnmp/log/nginx/access.log \ --user=www \ --group=www \ --with-http_realip_module \ --with-http_sub_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre ➜ make ➜ make install ➜ cd /lnmp/server ➜ ln -s /lnmp/server/nginx-1.6.3/ nginx ➜ ls -l
提示:编译安装过程中可以使用命令 # echo $?
检 configure
/ make
的执行结果,0
表示成功.
3.4. 启动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ➜ /lnmp/server/nginx/sbin/nginx -t nginx: the configuration file /lnmp/server/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /lnmp/server/nginx-1.6.3//conf/nginx.conf test is successful ➜ /lnmp/server/nginx/sbin/nginx ➜ lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 38366 root 6u IPv4 93417 0t0 TCP *:http (LISTEN) nginx 38367 nginx 6u IPv4 93417 0t0 TCP *:http (LISTEN) ➜ netstat -tlunp|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 38366/nginx ➜ wget 127.0.0.1 ➜ curl 127.0.0.1
注:当浏览器无法访问时,尝试修改 SELinux、iptables
3.5. 添加快捷启动脚本 ,之后可以通过 service nginx xxx 方式控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ➜ vim /etc/init.d/nginx ➜ chmod 755 /etc/init.d/nginx ➜ service nginx Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest} ➜ service nginx configtest nginx: the configuration file /lnmp/server/nginx/conf/nginx.conf syntax is ok nginx: configuration file /lnmp/server/nginx/conf/nginx.conf test is successful ➜ service nginx start Starting Nginx: [ OK ]
3.6. 设置开机自启动 1 2 3 4 ➜ chkconfig --add nginx ➜ chkconfig nginx on ➜ chkconfig --list|grep nginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4. 安装 PHP 4.1. 检查 MySQL、Nginx是否正常
4.2. 检查安装 PHP 所需的 lib 库 1 2 3 ➜ rpm -qa zlib-devel libxm2-devel libjpeg-turbo-devel libpng-devel gd-devel libcurl-devel libxslt-devel freetype-devel zlib-devel-1.2.3-29.el6.x86_64 ➜ yum -y install zlib-devel libxm2-devel libjpeg-turbo-devel libpng-devel gd-devel libcurl-devel libxslt-devel freetype-devel
⚠️ 注意:默认 yum 源没有 libiconv-devel
这个包,需要按照下面的步骤单独编译安装:
1 2 3 4 5 6 7 8 9 ➜ cd /root/src ➜ wget http://ftp.gnu.org/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 ➜ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ➜ yum -y install libmcrypt-devel mhash mcrypt
4.3. 安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ➜ cd /root/src ➜ tar zxvf php-5.5.36.tar.gz && cd php-5.5.36 ➜ ./configure \ --prefix=/lnmp/server/php5.5.36 \ --with-config-file-path=/lnmp/server/php5.5.36/etc \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --enable-exif \ --with-gd \ --enable-gd-native-ttf \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-gettext \ --with-iconv-dir=/usr/local/libiconv \ --enable-mbstring \ --with-mcrypt \ --with-mhash \ --with-openssl \ --enable-bcmath \ --enable-pcntl \ --enable-shmop \ --enable-sysvsem \ --enable-soap \ --with-libxml-dir \ --enable-sockets \ --with-curl \ --enable-ftp \ --with-zlib-dir \ --enable-zip \ --with-pear ➜ ln -s /lnmp/server/php5.5.36/ /lnmp/server/php ➜ ll php.ini* -rw-r--r-- 1 1001 1001 69236 5月 25 17:36 php.ini-development <------- 开发模式,更多开启日志、调试信息 -rw-r--r-- 1 1001 1001 69266 5月 25 17:36 php.ini-production <------- 生产模式 ➜ cp php.ini-production /lnmp/server/php/etc/php.ini
4.4. 配置 PHP 服务(php-fpm 方式) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ➜ cd /lnmp/server/php/etc/ ➜ ls pear.conf php-fpm.conf.default ➜ cp php-fpm.conf.default php-fpm.conf ➜ /lnmp/server/php/sbin/php-fpm ➜ ps -ef|grep php-fpm root 2270 1 0 00:39 ? 00:00:00 php-fpm: master process (/lnmp/server/php5.5.36/etc/php-fpm.conf) www 2271 2270 0 00:39 ? 00:00:00 php-fpm: pool www www 2272 2270 0 00:39 ? 00:00:00 php-fpm: pool www root 2274 2227 0 00:39 pts/0 00:00:00 grep php-fpm ➜ lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME php-fpm 2270 root 7u IPv4 12844 0t0 TCP localhost:cslistener (LISTEN) php-fpm 2271 www 0u IPv4 12844 0t0 TCP localhost:cslistener (LISTEN) php-fpm 2272 www 0u IPv4 12844 0t0 TCP localhost:cslistener (LISTEN) ➜ vim /etc/init.d/php-fpm ➜ chmod 755 /etc/init.d/php-fpm ➜ service php-fpm start|stop|restart|reload|configtest
4.5. 配置 Nginx 支持 PHP 程序请求访问 1 2 3 4 5 6 7 8 9 10 11 12 server { listen 80 ; server_name localhost; root /lnmp/www/; index index.html index.htm index.php; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index.php; include fastcgi.conf; } }
5. 附录(软件下载连接) 5.1. MySQL
5.2. Nginx
5.3. PHP