|
| 1 | + |
| 2 | +# Sonarqube Setup |
| 3 | + |
| 4 | +SonarQube is an open-source static testing analysis software, it is used by developers to manage source code quality and consistency. |
| 5 | +## 🧰 Prerequisites |
| 6 | +1. Need an EC2 instance (min t2.small) |
| 7 | +2. Install Java-11 |
| 8 | + ```sh |
| 9 | + apt-get update |
| 10 | + apt-get list | grep openjdk-11 |
| 11 | + apt-get install openjdk-11-jdk -y |
| 12 | + ``` |
| 13 | + |
| 14 | +## Install & Setup Postgres Database for SonarQube |
| 15 | +`Source: https://www.postgresql.org/download/linux/ubuntu/` |
| 16 | +1. Install Postgres database |
| 17 | + ``sh |
| 18 | + sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' |
| 19 | + wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - |
| 20 | + sudo apt-get update |
| 21 | + sudo apt-get -y install postgresql |
| 22 | + ``` |
| 23 | +
|
| 24 | +1. Set a password and connect to database (setting password as "admin" password) |
| 25 | + ```sh |
| 26 | + sudo passwd postgres |
| 27 | + su - postgres |
| 28 | + ``` |
| 29 | + |
| 30 | +1. Create a database user and database for sonarque |
| 31 | + ```sh |
| 32 | + createuser sonar |
| 33 | + psql |
| 34 | + ALTER USER sonar WITH ENCRYPTED PASSWORD 'admin'; |
| 35 | + CREATE DATABASE sonarqube OWNER sonar; |
| 36 | + GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar; |
| 37 | + ``` |
| 38 | + |
| 39 | +1. Restart postgres database to take latest changes effect |
| 40 | + ```sh |
| 41 | + system restart postgresql |
| 42 | + system status postgresql |
| 43 | + ``` |
| 44 | +`check point`: You should see postgres is running on 5432 |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +apt install net-tools |
| 49 | + |
| 50 | +`Source: https://docs.sonarqube.org/latest/requirements/requirements/` |
| 51 | + |
| 52 | +1. Added below entries in `/etc/sysctl.conf` |
| 53 | + ```sh |
| 54 | + vm.max_map_count=524288 |
| 55 | + fs.file-max=131072 |
| 56 | + ulimit -n 131072 |
| 57 | + ulimit -u 8192 |
| 58 | + ``` |
| 59 | +1. Add below entries in `/etc/security/limits.conf` |
| 60 | + ```sh |
| 61 | + sonarqube - nofile 131072 |
| 62 | + sonarqube - nproc 8192 |
| 63 | + ``` |
| 64 | + |
| 65 | +1. reboot the server |
| 66 | + ```sh |
| 67 | + init 6 |
| 68 | + ``` |
| 69 | + ========================================== |
| 70 | + |
| 71 | + ## SonarQube Setup |
| 72 | + |
| 73 | +1. Download [soarnqube](https://www.sonarqube.org/downloads/) and extract it. |
| 74 | + ```sh |
| 75 | + wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.2.46101.zip |
| 76 | + unzip sonarqube-8.9.2.46101.zip |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Update sonar.properties with below information |
| 80 | + ```sh |
| 81 | + sonar.jdbc.username=<sonar_database_username> |
| 82 | + sonar.jdbc.password=<sonar_database_password> |
| 83 | + |
| 84 | + #sonar.jdbc.username=sonar |
| 85 | + #sonar.jdbc.password=admin |
| 86 | + #sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube |
| 87 | + #sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError |
| 88 | + `` |
| 89 | + |
| 90 | +1. Create a `/etc/systemd/system/sonarqube.service` file start sonarqube service at the boot time |
| 91 | + ```sh |
| 92 | + cat >> /etc/systemd/system/sonarqube.service <<EOL |
| 93 | + [Unit] |
| 94 | + Description=SonarQube service |
| 95 | + After=syslog.target network.target |
| 96 | +
|
| 97 | + [Service] |
| 98 | + Type=forking |
| 99 | + User=sonar |
| 100 | + Group=sonar |
| 101 | + PermissionsStartOnly=true |
| 102 | + ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start |
| 103 | + ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop |
| 104 | + StandardOutput=syslog |
| 105 | + LimitNOFILE=65536 |
| 106 | + LimitNPROC=4096 |
| 107 | + TimeoutStartSec=5 |
| 108 | + Restart=always |
| 109 | +
|
| 110 | + [Install] |
| 111 | + WantedBy=multi-user.target |
| 112 | + EOL |
| 113 | + ``` |
| 114 | +
|
| 115 | +1. Add sonar user and grant ownership to /opt/sonarqube directory |
| 116 | + ```sh |
| 117 | + useradd -d /opt/sonarqube sonar |
| 118 | + chown -R sonar:sonar |
| 119 | + ``` |
| 120 | +
|
| 121 | +1. Reload the demon and start sonarqube service |
| 122 | + ```sh |
| 123 | + systemctl daemon-reload |
| 124 | + systemctl start sonarqube.service |
| 125 | + ``` |
| 126 | +
|
| 127 | +
|
| 128 | +## 🧹 CleanUp |
| 129 | +
|
| 130 | + Stop sonarqube services and delete the instance |
| 131 | +
|
| 132 | + ## Unable to access Sonarqube from browser? |
| 133 | +
|
| 134 | + 1. Make sure port 9000 is opened at security group leave |
| 135 | + 2. start sonar service as a sonar user |
| 136 | + 3. user correct database credentials in the sonar.properties |
| 137 | + 4. user instance which has atleast 2 GB of RAM |
| 138 | + |
| 139 | +
|
| 140 | + |
| 141 | +## 🔗 My Profile |
| 142 | +[](https://www.udemy.com/user/ar-shankar/) |
| 143 | +[](https://www.linkedin.com/in/ravdsun/) |
| 144 | +
|
| 145 | +
|
| 146 | + ### 💡 Help/Suggestions or 🐛 Bugs |
| 147 | +
|
| 148 | +Thank you for your interest in contributing to our project. Whether it is a bug report, new feature, correction, or additional documentation or solutions, we greatly value feedback and contributions from our community. [Start here](/issues) |
0 commit comments